"command not found" when using arithmetic expansion in bash shell

Using Ubuntu Desktop, I have terminal open and I am using the bash shell. One of the shell expansions of bash is the arithmetic expansion, with the following syntax:

$(( EXPRESSION ))
or 
$[ EXPRESSION ]

When i do arithmetic, it does return the correct value but it is always followed by "command not found":

$ $((1+2))
3: command not found
$ $[1+2]
3: command not found
$ $[2+2]
4: command not found
$ $((2*6))
12: command not found

My question is why does it display "command not found" and how can I fix that?


You have to add echo command before all of your commands,

$ echo $[1+2]
3

You don't have to put directly $[1+2] on terminal, because bash computes $[1+2] and again parses the same, so command not found error occurs.

For Example

$ var="sudo apt-get update"
$ $var

Ign http://archive.canonical.com saucy InRelease                               
Ign http://ppa.launchpad.net saucy InRelease                                   
Ign http://ubuntu.inode.at saucy InRelease                          
Ign http://extras.ubuntu.com saucy InRelease                        
29% [Waiting for headers] [Waiting for headers] [Waiting for headers]

In the above example, sudo apt-get update command was assigned to a variable var.On running $var, first bash expands it and again parses the expanded one.


$ $((1+2))
3: command not found

What is happening here is that bash computes $((1+2)) which results in 3. bash then looks for a command named 3 to execute. It doesn't find it. Hence the error. As @Avinash suggests, use echo to avoid this.

$ echo  $((1+2))
3

Because bash is trying to execute the output of your expansion and it doesnt find any command with name 3 in the PATH. To just try out, use echo or assign it to a variable and use it later.

echo $((1+2))
3
test=$((1+2))
echo $test
3