Search code examples
bashshellcentos6.5

Cannot assign dot to variable in Bash shell


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?


Solution

  • 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: