2

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?

hirikarate
  • 3,055
  • 4
  • 21
  • 27
  • 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. – fedorqui Apr 08 '15 at 11:04

2 Answers2

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:

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • 1
    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 Apr 08 '15 at 11:48
  • @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. – hek2mgl Apr 08 '15 at 12:21
  • 1
    (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))`. – chepner Apr 08 '15 at 15:34
  • 1
    (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.) – chepner Apr 08 '15 at 15:36
  • `According to the man page, let "expression" is identical to ((expression))` This was what I needed. I just wasn't exactly sure. Many thanks! – hek2mgl Apr 08 '15 at 15:42
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
Arjun Mathew Dan
  • 5,240
  • 1
  • 16
  • 27