3

I run this command both by typing in Terminal and by executing file on CentOS 6.5 64 bits.

let t='.'; echo $t;

Can't believe that it yields such a wierd error:

-bash: let: t=.: syntax error: operand expected (error token is ".")

As far as I know, single-quoted strings should not be parsed. In fact, in the first place, what I wanted to do is:

let $target_path='./files/mp4';

Can anyone please explain this behavior and guide me to stop this wierd act?

1
  • Just use target_path='./files/mp4'. Variables in bash are set like var="value", not $var="value". To use then later on, you do use the dollar: echo "$var". Also, there is no need to use ; at the end of the line. Commented Apr 8, 2015 at 11:04

2 Answers 2

10

Unless you want to evaluate the variable value as an arithmetical expression, you need to assign the variable value like this:

t='.'

let is for calculation of arithmetical expressions and . or ./files/mp4 produces a syntax error in that arithmetical expression. Check help let.


Here comes an example how let can be used:

a="10*2"
echo "$a" # Prints: 10*2

let a="10*2"
echo "$a" # Prints: 20

If you followed the discussion below you may have noticed that even for mathematical expressions let isn't the best choice. This is because you can use ARITHMETIC EXPANSION in that case which is defined by POSIX in opposite to let. Using ARITHMETIC EXPANSION the above example would look like this:

a=$((10*2))
echo "$a" # Prints: 20

Check this articles for further information:

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

5 Comments

Also, note that let is generally necessary, as you can also write a=$((10 * 2)) (which has the benefit of being part of the POSIX standard).
@chepner I'm not sure if let and arithmetic expansion can do the same. I guess not. The documentation doesn't explicitly state that. Also OP is searching for a string assignement. That's why I don't wanted to point to alternatives to let, i justed wanted to show the basic difference between a let assignment and a string assignment.
(I do have a typo, should be "let is generally unnecessary"). At the very least, let is superseded by the (( ... )) command. According to the man page, let "expression" is identical to ((expression)).
(And +1, since at least in this case regular assignment is what you need. Just wanted to point out the let itself is probably never needed, and one would do just fine not knowing it existed to avoid this type of error.)
According to the man page, let "expression" is identical to ((expression)) This was what I needed. I just wasn't exactly sure. Many thanks!
0

Using let here is not right. You can do like this:

AMD$ t='.'
AMD$ echo $t
.

AMD$ t='./files/mp4'
AMD$ echo $t
./files/mp4

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.