-1

a.sh content

#!/bin/bash

awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }'  <(grep 'cpu ' /proc/stat) <(sleep 5;grep 'cpu ' /proc/stat)


It works when pasted to shell directly,but not work when executed as a script file sh a.sh. what is the difference ?

fireboy
  • 29
  • 5
  • 3
    Duplicate of [Difference between sh and bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – oguz ismail May 21 '20 at 06:27

1 Answers1

0

This most likely mean your default shell is not "/bin/bash". You can see which shell is your default shell with this command:

echo $SHELL

If you never changed it, chances are that it is "/bin/sh".

Since it works in your shell, the easiest is to change your shebang line at the top so that it reference the path of your default shell instead of "/bin/bash".

But if you end up wanting to make it work in bash and change your default shell to Bash, this is how you do it:

chsh -s /bin/bash
Mig
  • 662
  • 4
  • 13