Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting a 'unary operator expected' error?

Tags:

bash

shell

I'm writing a shell script to streamline my development workflow.

It takes an argument as to which theme folder I'm going to be working in and starts grunt watch on that directory.

If I call the script without the necessary argument I'm currently printing a warning that a theme needs to be specified as a command line argument.

I'd like to print a list of the available options, e.g. theme directories

This is what I have so far...

THEME=$1

if [ $THEME == '' ]
then
    echo 'Need to specify theme'
else
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
fi

Ideally I'd replace the output of the echo line with an ls of the themes parent directory like so

THEME=$1

if [ $THEME == '' ]
then
    echo 'Need to specify theme from the following'
    ls workspace/aws/ghost
else
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
fi

However this gives me the following error

./ghost_dev.sh: line 3: [: ==: unary operator expected
like image 829
Luke Avatar asked Oct 30 '13 18:10

Luke


People also ask

How do I fix unary operator expected?

Another way to avoid encountering the unary operator expected error on our terminal shell is to use the double “square” brackets at the start and end of the “if” condition while using the “-eq” comparison operator for any type of value. So, we did that as shown below.

What is unary operator error shell script?

What is the Bash Unary Operator Expected error? It's an error that occurs when Bash identifies a line in your script that contains a binary operator that is not applied to two arguments. When this happens Bash assumes that you want to use a unary operator instead and raises the Unary Operator Expected error.

What is unary operator example?

In mathematics, an unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function f : A → A, where A is a set.

What is unary operator in JavaScript?

A unary operation is an operation with only one operand. This operand comes either before or after the operator. Unary operators are more efficient than standard JavaScript function calls. Additionally, unary operators can not be overridden, therefore their functionality is guaranteed.


2 Answers

You need quotes around $THEME here:

if [ $THEME == '' ]

Otherwise, when you don't specify a theme, $THEME expands to nothing, and the shell sees this syntax error:

if [ == '' ]

With quotes added, like so:

if [ "$THEME" == '' ]

the expansion of an empty $THEMEyields this valid comparison instead:

if [ "" == '' ]

This capacity for runtime syntax errors can be surprising to those whose background is in more traditional programming languages, but command shells (at least those in the Bourne tradition) parse code somewhat differently. In many contexts, shell parameters behave more like macros than variables; this behavior provides flexibility, but also creates traps for the unwary.

Since you tagged this question bash, it's worth noting that there is no word-splitting performed on the result of parameter expansion inside the "new" test syntax available in bash (and ksh/zsh), namely [[...]]. So you can also do this:

if [[ $THEME == '' ]]

The places you can get away without quotes are listed here. But it's a fine habit to always quote parameter expansions anyway except when you explicitly want word-splitting (and even then, look to see if arrays will solve your problem instead).

It would be more idiomatic to use the -z test operator instead of equality with the empty string:

if [ -z "$THEME" ]

You technically don't need the quotation marks in this simple case; [ -z ] evaluates to true. But if you have a more complicated expression, the parser will get confused, so it's better to just always use the quotes. Of course, [[...]] doesn't require any here, either:

if [[ -z $THEME ]]

But [[...]] is not part of the POSIX standard; for that matter, neither is ==. So if you care about strict compatibility with other POSIX shells, stick to the quoting solution and use either -z or a single =.

like image 81
Mark Reed Avatar answered Nov 11 '22 16:11

Mark Reed


[ "$THEME" ] will evaluate to false if $THEME is undefined or an empty string and true otherwise. See http://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html#Bash-Conditional-Expressions. You can rearrange your if statement to exploit this behavior and have an even simpler conditional:

if [ "$THEME" ]; then
    cd 'workspace/aws/ghost/'$THEME'/'
    grunt watch
else
    echo 'Need to specify theme from the following'
    ls workspace/aws/ghost
fi

"$THEME" needs to be in double-quotes, in case its value contains whitespace.

like image 22
Digital Trauma Avatar answered Nov 11 '22 16:11

Digital Trauma