Thursday, May 2, 2024
5
rated 0 times [  5] [ 0]  / answers: 1 / hits: 6942  / 1 Year ago, sat, november 26, 2022, 4:02:15

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?


More From » command-line

 Answers
3

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.


[#26501] Sunday, November 27, 2022, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kneducator

Total Points: 226
Total Questions: 111
Total Answers: 108

Location: Mexico
Member since Sun, Jul 25, 2021
3 Years ago
;