2

I'm using GNU bash, versión 5.0.3(1)-release (x86_64-pc-linux-gnu), and I wonder why a simple assignment statement gives a syntax error:

#/bin/bash

var1=/tmp       # This works
let var2=/tmp   # This fails with syntax error: Operand expected (error is "/tmp")

Does anybody know why? Thanks

1
  • help let should show you why... Commented May 5, 2020 at 10:05

2 Answers 2

2

let is used only for arithmetic operations, just as (( )).

Therefore, the following lines are the same

a=$((60 * 60))
let a=(60*60)
((a = 60 * 60))

See the manpage.

Sign up to request clarification or add additional context in comments.

Comments

1

let is a shell builtin that is a another way to use the $((..)) Arithmetic Expansion in bash. The assignment

let var2=/tmp 

is treated by the shell as an arithmetic operation "division" with an incorrect quotient value. It is equivalent to doing.

var=$((/tmp))

Since there are incorrect number of operands, the parser has thrown the error that you are seeing. Note that tmp is still treated in a variable context by the shell. If the parser had identified the expression as valid, then tmp would have undergone variable expansion. Since it is not set before, it would have likely thrown a "division-by-zero" error.

For simple variable assignments just drop the let keyword and enclose the value field within "quotes"

var2="/tmp"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.