Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

What's the meaning of a trailing colon to dash's builtin test (or [) command? I was surprised when I found that if [ ... ]: ; then .. ; fi works (at all) in dash, but not bash, and it seems to be because the builtin's behavior differs from the command's.

$ /usr/bin/\[ 1 ] && echo 1
1
$ /usr/bin/\[ 1 ]: && echo 1
/usr/bin/[: missing `]'
$ [ 1 ]: && echo 1
1
share|improve this question
1  
What does [ 1 ]foo && echo 1 do? My wild initial guess is that dash is checking whether the argument starts with ] instead of whether it's equal to ]. –  godlygeek Mar 26 at 19:07
    
Looks like [ 1 ]foo && echo 1 prints 1, so @Arcege's right. –  David Ehrmann Mar 26 at 19:19

1 Answer 1

up vote 2 down vote accepted

It seems to be more of an issue if parsing the commands. bash expects the closing bracket to be the last argument and complains if it not exactly a "]" string.

bash$ [ 1 ]
bash$ [ 1 ] hi
bash: [: missing `]'
bash$ [ 1 ]hi
bash: [: missing `]'
bash$ dash
$ [ 1 ]
$ [ 1 ] hi
dash: 2: [: missing ]
$ [ 1 hi]
dash: 3: [: missing ]
$ [ 1 ]:
$ [ 1 ]hi
$

The dash shell will just make sure that the first character is "]" and ignore the rest. If the "]" is not in the first character of the last argument, then it returns a similar error as bash.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.