Ubuntu – “command not found” when using arithmetic expansion in bash shell

bashcommand line

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?

Best Answer

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.