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.

I'm trying to prompt the user for a comment

function comment {
  if [ ! $1 ]
  then
   read -p "enter checkin comment < " COMMENT;     
  else
   export COMMENT="$1"
  fi 

  if [ ! $COMMENT ]
  then
    echo 'no comment!'
  else
    echo "$COMMENT";
  fi  
}

UPDATED This happens when I enter more than one word into the prompt, if I enter "update" for example I dont get an error message... but if I enter more than one word

enter checkin comment >> sun mars venus

then I get the error (always on the second word)

-bash: [: mars: binary operator expected
share|improve this question
    
Are those lines the whole thing? No error here. –  enzotib Sep 12 '11 at 19:03
    
No error here too. –  user unknown Sep 12 '11 at 21:56
1  
The error is in another line which you haven't shown. Post the whole script. –  Gilles Sep 12 '11 at 21:57
    
What version of bash do you have? I've confirmed it works as written with double quotes on Cygwin's bash (Win7), bash (CentOS 5), and BusyBox (TomatoUSB). –  Aaron D. Marasco Sep 13 '11 at 2:04
    
Ive updated my post hopefully theres enough info there now to answer (CentOS 5/Bash) –  qodeninja Sep 13 '11 at 3:52
show 1 more comment

2 Answers

up vote 6 down vote accepted
if [ ! $COMMENT ]

I think you meant to check whether $COMMENT is non-empty, but that's not what this command does. An unquoted variable substitution undergoes filename generation (globbing) and word splitting. Here, you're entering several words in your comment (sun mars venus) so the [ command sees ! sun mars venus (4 arguments) which is not valid syntax. Always put double quotes around variable substitutions:

if [ ! "$COMMENT" ]

In this particular case, this tests whether $COMMENT is non-empty. This is a shortcut because there are only two shell words inside the brackets. In the general case, the way to test whether a string is non-empty is to use the -n operator, and the -z operator tests whether the string is empty.

if [ -z "$COMMENT" ]

In ksh/bash/zsh, you can use the [[ … ]] construct instead of the [ … ] command. The single brackets are an ordinary command bound by the usual shell syntax rules, whereas the double brackets are a special shell syntax with its own rules. There is no word splitting inside double brackets, so you can write

if [[ -z $COMMENT ]]

Double quotes wouldn't hurt though.

The same goes for if [ ! $1 ] which should be if [ -z "$1" ] or if [[ -z $1 ]].

There's an additional oddity that you export the COMMENT variable to the environment when the comment is passed as an argument to the function but not when you read it with the read built-in. Unless you need to pass COMMENT to an external program, drop the word export.

share|improve this answer
add comment

Bash is interpreting the[ as the test command (which requires spaces around the argument). See Greg's Wiki on tests & conditionals...

share|improve this answer
1  
The [ is inside a quoted string, so how can it be interpreted as the test command? –  enzotib Sep 12 '11 at 19:04
    
Based on the code provided, and the error message, that is the only conclusion I can draw. –  jasonwryan Sep 12 '11 at 19:47
    
Ive updated my post hopefully theres enough info there now to answer –  qodeninja Sep 13 '11 at 3:52
1  
You need to use quotes in your test - otherwise the words are split and bash sees more than one argument for test See: mywiki.wooledge.org/Quotes –  jasonwryan Sep 13 '11 at 4:16
add comment

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.