0

I have a .sh file. It includes bash syntax.

#!/bin/bash
function foo() {
    // do something
}

doo()
// do something

sh doesn't link to bash on my system.

Below command doesn't work:

sh sample.sh

It throws syntax error. Below command works fine.

bash sample.sh

I think '#!/bin/bash' is useless for my case. I know that sh != bash. But do I must specify sh/bash/etc like upper example to run .sh file?

Cyrus
  • 84,225
  • 14
  • 89
  • 153
zakjma
  • 2,030
  • 12
  • 40
  • 81

1 Answers1

0

The shebang (#!) is only used if you execute the file (i.e: ./sample.sh). Of course, the file must have execution permissions.

If you execute sh with your file as argument its content is interpreted by sh (not taking into account the shebang). The same for bash or any other command that reads the file represented by the first argument.

Gonzalo Matheu
  • 8,984
  • 5
  • 35
  • 58
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section "Answer Well-Asked Questions", and therein the bullet point regarding questions which "have been asked and answered many times before". – Charles Duffy Jun 07 '19 at 19:23