[colug-432] Shell Tests: Leading 0

jep200404 at columbus.rr.com jep200404 at columbus.rr.com
Thu Oct 15 10:33:24 EDT 2015


On Tue, 6 Oct 2015 18:21:53 -0400, Rob Stampfli <rob944 at cboh.org> wrote:

> 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'?

> 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.

I now see a similar thing for numbers.
Instead of prepending a letter, the digit zero is prepended.

    [aen at mad 1]$ cat count.sh 
    #!/bin/sh
    
    for file in *; do
        i=`expr "0$i" + 1`
        echo $i $file
    done
    [aen at mad 1]$ ./count.sh 
    1 count.sh
    2 foo
    3 hello
    4 world
    [aen at mad 1]$ 

Notice how i is not initialized before the loop. Yikes!


More information about the colug-432 mailing list