I have the following bash script:

while [ $loop == "true" ]
     //do stuff

but it says error at [.

Also this runs as a daemon, when the stop argument is passed to the script...the loop should. I'm guessing setting $loop to false will automatically end the loop.

  • "... it says error at [." Really? Jan 9 '12 at 1:11
  • Have a -1 for not giving nor properly describing the error message.
    – JdeBP
    Jan 9 '12 at 11:28

The best way to type this would be:

while [ "$loop" = "true" ]

Read the section "Working out the test-command" at the following URL:

  • 2
    All values in bash are strings. The quotes have nothing to do with that. Jan 9 '12 at 1:17
  • 2
    The quotes makes it work. And = compares strings, compared to (for example) -eq that compares integers. Check Advanced Bash Scripting Guide for more information "Note that integer and string comparison use a different set of operators.": tldp.org/LDP/abs/html/comparison-ops.html Jan 9 '12 at 1:21
  • Or well, you are right, the "" in itself doesn't make it a string, you might aswell use: $loop = true but bash still makes difference between strings and integers. Updated the answer slightly. Jan 9 '12 at 1:23
  • 6
    You know, telling someone they are wrong without explaining and properly correcting is kindof pointless and dumb? Jan 9 '12 at 1:27
  • 1
    Or as GreyCat would have said: Use More Quotes!
    – l0b0
    Jan 9 '12 at 11:55

M. Vazquez-Abrams is quite right. This is nothing to do with quotation marks making things that are already strings into strings, or some wrongheaded idea that = in bash's built-in [ command is anything other than a string comparison. (Read § 6.4 of the Bash User Manual, people!) It's everything to do with what happens to empty fields after field-splitting turns words into fields.

If the shell variable loop is empty or null, then $loop expands to an empty field. After field splitting, empty fields are discarded. Note that field splitting and the check for empty fields precedes quote removal. So "$loop" expands to the field "", which is not empty and is thus not removed. After quote removal it is then an empty field, that becomes an empty argument to the command.

The [ command requires its = operator to have two operands, fore and aft. Anything else is a syntax error. Since an empty field is removed, the sequence of words

[ $loop = true ]
expands to four fields

  1. [
  2. =
  3. true
  4. ]

when the [ command needs five to be syntactically correct:

  1. [
  3. =
  4. true
  5. ]

Of course, the empty string is not equal to the four-character string true, and the command's exit status is non-zero.

Again, all of this is in the Bash User Manual, in §3.5 and §3.5.7. The manual is your friend.

  • Oh yes, you are ofcourse right. Well put! I much more appreciate these types of corrections and explanations though than Mr Vazquez-Abrams arrogant comments just pointing out that someone is wrong without bothering to explain or clarify. Hardly helpful. I phrased myself badly with converting to string, but I meant there is a difference between strings and integers in how you're supposed to loop them. And I always have a habit of putting my tests/strings within "" to handle spaces properly. :) I also wrongly assumed that $loop was set to "true", while it might not have been. Jan 9 '12 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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