What does "unary operator expected" mean
From Linuxintro
Revision as of 20:05, 16 December 2014 by imported>ThorstenStaerk
When you work with Linux scripts on the command line, you will sometimes get an error message saying
unary operator expected
This means that there is a comparison where one site is empty for example
if [ $name = "Thorsten" ]
and $name is empty. Then the bash shell internally replaces $name by an empty string and it will be interpreted as
if [ = "Thorsten" ]
and this is not a valid expression.
The solution is to quote variable names like this:
if [ "$name" = "Thorsten" ]
Then it will work.
Shell scripting tutorial
Try the shell scripting tutorial to avoid this mistake and similar ones 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 + '[' = Thorsten ']' test.sh: line 3: [: =: unary operator expected