0

I'm quiet new to scripting and I struggle a bit with this command, maybe someone can help

When I use this command, everything's works

titi=`echo 'scale=6; 10/65535' | bc -l | awk '{printf "%.6f\n", $0}'
echo "titi = $titi "

And I get this

titi = 0.000152

Perfect

But when I try to use a variable in it, I got an error:

toto=10
titi=`echo 'scale=6; $toto/65535' | bc -l | awk '{printf "%.6f\n", $0}'` 
echo "titi = $titi "

Result

(standard_in) 1: illegal character: $

So it's obvious the way I write the variable in the command is wrong, but I don't know how to write it

Can someone can help me?

1 Answer 1

3

Use double quotes to expand variables and note that $() is recommended over backticks in command substitution. It should be:

$ toto=10
$ titi=$(echo "scale=6; $toto/65535" | bc -l | awk '{printf "%.6f\n", $0}')
$ echo "titi = $titi "
titi = 0.000152

(notice that $ here is a command line prompt used to indicate start of a new line, not a part of the command).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .