Sleep command in sh with variables Sleep command in sh with variables unix unix

Sleep command in sh with variables


As others have indicated, the modern(*) approach is to use #!/bin/bash or even #!/bin/ksh with their built-in support of arithmetic operations using n=$(($a+$b+$c).

As you have explicitly mentioned #!/bin/sh, here is a solution that will work for you.

#!/bin/shecho hello1a=6b=2c=3d=`echo "$a+$b+$c" | bc`sleep $decho hello2

The key part is using backquotes for command-substitution, and sending the math arguments to be evaluated by the bc command, using the shell's pipe (|) functionality: i.e.

d=`echo "$a+$b+$c" | bc` |  |       |      |  | -> bc is a "basic" calulator |  |       |      | -> pipes pass std-out from preceding cmd, to std in fo following cmd |  |       | -> a string to be passed to `bc` for arthimetic evaluation |  | -> echo writes its output to std-out  | -> = assigns output of cmd=substition to var $d

(*) modern in shells means 1986 and later ;-)

IHTH


You can do arithmetic with $((...)):

d=$(($a + $b + $c))


Try this script:

#!/bin/bashecho "hello1";a=6;b=2;c=3;d=$(($a+$b+$c));sleep $d;echo "hello2";