23 September, 2010

Born again

Few interesting things I've recently discovered:

bar ()
{
    exit 1
}

foo ()
{
    var=`bar`
    [[ $? -ne 0 ]] && echo "bar() failed"
   
    # Now try local
    local var=`bar`
    [[ $? -ne 0 ]] && echo "bar() failed again"
}

You will not see "bar() failed again", as you can expect. Second time "$?" will be an exit status of local like I was explained by much more experienced engineer.

Another unexpected thing is about re in grep:
echo -e "foobar" | grep "^\(foo\)\{0,1\}bar"
echo -e "foobar" | grep "^\(foo\)\{0,0\}bar"
Explanation is simple: {0, 0} is zero-length substring, which means anything before "bar". You can't achieve equivalent of "-v" flag with {0}.

No comments: