Difference between revisions of "What does "unary operator expected" mean"
From Linuxintro
imported>ThorstenStaerk |
imported>ThorstenStaerk |
||
Line 12: | Line 12: | ||
= Shell scripting tutorial = | = Shell scripting tutorial = | ||
− | Try the tutorial [[BaBE - | + | Try the tutorial [[BaBE - Bash by Examples]] to avoid this and similar mistakes in the future. |
= Debugging bash scripts = | = Debugging bash scripts = |
Revision as of 16:44, 23 December 2014
The bash message
unary operator expected
means that you do a comparison where one site is empty for example
if [ $name = "foo" ]
and $name is empty. Then the bash shell internally replaces $name by an empty string and it will be interpreted as
if [ = "foo" ]
and this is not a valid expression.
The solution is to quote variable names like this:
if [ "$name" = "foo" ]
Then it will work.
Shell scripting tutorial
Try the tutorial BaBE - Bash by Examples to avoid this and similar mistakes in the future.
Debugging bash scripts
You can also debug the script line-by-line using bash -x. bash -x shows all commands that are being executed, just like gdb or strace, but for bash scripts:
tweedleburg:~ # bash -x test.sh + echo 'how is your name? ' how is your name? + read name + '[' = foo ']' test.sh: line 3: [: =: unary operator expected