Score:2

Evaluate string ```$((expression))```-like and save in a variable

in flag

This will sound pretty awkward for some of you. Suppose I have a string which represents some $((expression)), arithmetic expression mathematical like '$((1+1))'. I wish to perform it and deliver its result to a variable.

Example:

$ expression='$((5+5))'
$ echo $expression 
'$((5+5))'

$ # Expected result: 10

How can I evaluate that string and store its result?

Bruno Henrique Peixoto avatar
in flag
Oh, I may cook a great pasta with all that macarronic mistakes, thanks!
Score:1
jp flag

If you're evaluating a string try eval.

str="$[ 5 + 5 ]"
eval "numb=$str"    # Turns into `numb=$[ 5 + 5 ]`
echo $numb    # Outputs 10

eval can execute strings as if they were shell code. Be careful! If $str can contain special characters it may break your script or even create a security hole.

Score:0
sd flag

Just store it in variable like you would normally do.

$ result=$((5+5))
$ echo $result 
10

If this is not what you meant, edit your post and post additional information.

Bruno Henrique Peixoto avatar
in flag
I updated the question for better undestannding of my case. But I appreciate the answers sofar! :-)
jp flag
Dan
@BrunoHenriquePeixoto In your edit, you used almost the same code. This answer is exactly what you want in this case. The only error you have is that use the quotes on the `$((5+5))` part, you need to remove that.
Bruno Henrique Peixoto avatar
in flag
Well, the operation I need to evaluate has quotes. Then, I am afraid I cannot evaluate them.
Score:0
cn flag

If it is actually mathematical, specifically if it involves simple integer arithmetic, you can use exactly the format you have:

$ var=$((1+1))
$ echo "$var"
2

The same goes if it is a command. For example, slightly more complicated math can be done by piping to bc:

## simple integer math works
$ echo $((3 * 2))
6
## fractions fail
$ echo $((1.3 * 2))
bash: 1.3 * 2: syntax error: invalid arithmetic operator (error token is ".3 * 2")
## bc works
$ echo "1.3 * 2" | bc
2.6

So, to store that in a variable, you just use standard command substitution:

$ var=$(echo "1.3 * 2" | bc)
$ echo "$var"
2.6

Finally, you aso have the eval builtin (see help eval in bash):

$ help eval
eval: eval [arg ...]
    Execute arguments as a shell command.
    
    Combine ARGs into a single string, use the result as input to the shell,
    and execute the resulting commands.
    
    Exit Status:
    Returns exit status of command or success if command is null.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.