[colug-432] Shell Tests

Rob Stampfli rob944 at cboh.org
Tue Oct 6 18:21:53 EDT 2015


On Tue, Oct 06, 2015 at 05:21:56PM -0400, jep200404 at columbus.rr.com wrote:
> Which shell test would you choose? Why?
> Would you choose a test not shown below? Why?
> 
> I saw a shell test like [ "z${1}" = "z" ], 
> which got me wondering, why bother with the 'z'?
> Is there some subtle behavior, perhaps when there 
> is no "${1}" argument? So I experimented. 
> 
> These tests worked the same:
> 
>     [ "${1:+is_set}" != "is_set" ]
>     [ "z${1}" = "z" ]
>     [ "${1}" = "" ]
> 
> The last of those above is curious.
> Since it works the same as with 'z', why bother with the 'z'?

It used to be that the "test" command got confused when the first
argument started with a dash, so shell programmers routinely
prepended an alpha to cover this case.

Rob


More information about the colug-432 mailing list