Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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?

share|improve this question

3 Answers 3

up vote 4 down vote accepted

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.

share|improve this answer
$ $((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
share|improve this answer

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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.