My program is#!/bin/shprintf 'Please enter salary amount: 'read salaryTAX=echo $salary * 0.15 | bcNET=echo $salary + $TAX | bcprintf Net Salary= %.2f $NETprintf \nBut whatever I try I can't get the output I expect.Right now I am getting$ . 3_net_salary.sh Please enter salary amount: 300-bash: printf: echo: invalid number-bash: printf: +: invalid number-bash: printf: echo: invalid number-bash: printf: 1_simple.awk: invalid number-bash: printf: 1_simple_awk.sh: invalid number-bash: printf: 2_simple_awk.sh: invalid number-bash: printf: 2_simple_before.awk: invalid number-bash: printf: 3_net_salary.sh: invalid number-bash: printf: 4_purchase_amounts.sh: invalid number-bash: printf: alert_spec.rb: invalid number-bash: printf: awk_passwd.sh: invalid number-bash: printf: numbers.txt: invalid number-bash: printf: sed_to_change_should_syntax_to_expect-to: invalid number-bash: printf: should_to_expect: invalid number-bash: printf: t1_spec.rb: invalid number-bash: printf: test001.txt: invalid number-bash: printf: test_1_spec.rb: invalid number-bash: printf: test_2_spec.rb: invalid number-bash: printf: |: invalid number-bash: printf: bc: invalid number-bash: printf: |: invalid number-bash: printf: bc: invalid numberNet Salary= 0.00Net Salary= 300.00Net Salary= 0.00Net Salary= 0.00Net Salary= 300.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.15Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00Net Salary= 0.00
How to use printf to show the results of a calculation?
shell script;printf
You are not doing what you think you're doing. This command:TAX=echo $salary * 0.15 | bcDoes not run anything, there is no calculation. That assigns a string to the variable $TAX. Then, because you're not quoting the variable when you pass it to printf, the * will expand to everything in the current directory. To illustrate:$ lsfile1  file2foo=echo 12 * 4 | bc$ echo $fooecho 12 file1 file2 4 | bcAs you can see above, the * expanded to the contents of the directory and no calculation was made. In order to assign the result of a command to a variable, you should use var=$(command), not quotes.So, a working version of your script would be:#!/bin/shprintf 'Please enter salary amount: 'read salaryTAX=$(printf '%s * %s\n' $salary 0.15 | bc)NET=$(printf '%s + %s\n' $salary $TAX | bc)printf Net Salary= %.2f\n $NETWhen debugging something you should always think of printing your variables to understand what they contain.