1

Why does

a2=100.    
echo 1.1*$a2|bc   

work on the terminal, but the 3 variants in the following script don't:

a2=100.  
echo "  
1.1*$a2|bc  
$(1.1*$a2|bc)  
$((1.1*$a2|bc))  
">x.sh    
1

Because you didn't put echo statements in the script code piped to x.sh. The second variant requires an additional echo inside the subshell command $(...). And the third variant is not valid, because $((...)) arithmetic expansion doesn't work on floating point numbers.

a2=100.  
echo "  
echo 1.1*$a2|bc;  
echo $(echo 1.1*$a2|bc);
">x.sh    

Result:

# sh x.sh
110.0
110.0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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