I am almost new with bash. I am sorry if this question has been answered somewhere else but I didn't find anything which I could understand.

I am making an if condition:

if [ $inputstring = $INPUT ]

this line gives me a "too many arguments] error". I also tried:

  if [ "$inputstring" = "$INPUT" ]

but when I am sure the variables have the same value (through echos) my program doesn't go inside the "if".

can anyone help please? Thanks

  • 1
    can u plz share the value of both variables...
    – Siva
    Jul 22 '19 at 10:59

You would get the "too many arguments" if the variables contain whitespace, which would cause the unquoted expansions to split to multiple words. So, indeed, you must quote all variables within [ .. ] to prevent that.

The second if you wrote (if [ "$inputstring" = "$INPUT" ]) is correct, and would run the main branch of the if, if the two variables indeed contain the same data. But depending on where they come from, you might have extra whitespace within them, or something else that is hard to see. Use e.g. printf "<%q>\n" "$INPUT" to see the variable contents in an unambiguous format. (The output format depends on the value of the variable, but generally it prints it quoted and/or shows special characters with backslash-escapes.)

For example, the variables don't contain the same data here, the latter has a trailing space:

$ foo=123
$ bar="123 "
$ echo $foo $bar
123 123
$ if [ "$foo" = "$bar" ]; then echo same; else echo not same; fi
not same
$ printf "<%q>\n" "$foo" "$bar"
<123\ >

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.