I'm trying to prompt the user for a commentfunction comment {  if [ ! $1 ]  then   read -p enter checkin comment &lt;  COMMENT;       else   export COMMENT=$1  fi   if [ ! $COMMENT ]  then    echo 'no comment!'  else    echo $COMMENT;  fi  }UPDATEDThis 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 wordenter checkin comment >> sun mars venusthen I get the error (always on the second word)-bash: [: mars: binary operator expected
Unix read -p command causes bash error, how do I fix it?
bash;prompt
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 writeif [[ -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.