1

If we have a program start.sh, we can execute it with the sh start.sh. But we could execute it giving permission with chmod and run ./start.sh in the command line. What is the difference between those two ways of executing a bash/sh script.

Vitor_figm
  • 85
  • 1
  • 5

1 Answers1

1

They both execute shell script but there is some little difference:

sh will use will use the sh interpreter even if a differenter interpreter was indicated in the begining of the file like #!/bin/bash this can be useful if you want your script to be executed using always the same interpreter

for ./start.sh the shell will try to execute as an executable file so the script need a shebang like #!/bin/bash so ./start.sh is more flexible since you can use the interpreter you want

Saxtheowl
  • 1,609
  • 2
  • 9
  • 20
  • 1
    You don't _need_ a shebang actually. `./start.sh` will still work even without one. I was just testing and I can't quite figure out if it runs using whatever the default `$SHELL` is or not. When I was running a bash shell, it ran with `bash` but from a `zsh` and `dash` shell, it ran with `/bin/sh`. In any case, the main point is that even without a shebang, an executable file containing shell commands can be run with `./script.sh`. – terdon Mar 17 '23 at 11:19