-1

I wrote script with deafult permission -rw-r--r-- 1. If I run this sh script.sh it get executed, while excuting with ./script it throw permission denied.

What is the difference between both the commands? Why it's not required change in permission for 'sh script.sh'?

[root@ip safe]# sh test.sh

test

[root@ip safe]# ./test.sh

-bash: ./test.sh: Permission denied

  • The file isn't a "executable", so it cannot be executed. Running `sh ` will execute the command `sh`, it will be found by searching the `$PATH` environment variable, and then executed with two arguments: `sh` and `` in this case the `sh` command will interpret the provided file and run the commands. You can make a file executable with `chmod +x `. – Andreas Louv Mar 19 '22 at 15:11
  • 1
    Note that `sh` **is not bash**. Even when it's the same executable it runs in a limited compatibility mode with started under the `sh` name. If you want full bash capabilities, use `bash`, not `sh`. – Charles Duffy Mar 19 '22 at 15:13
  • Re: execute permissions, it's not specific to sh either. You don't need execute permissions for `python myscript.py`, but _do_ need them for `./myscript.py`; same for every other language (including compiled executables, for which the interpreter is `ld.so`). – Charles Duffy Mar 19 '22 at 15:15
  • 2
    Going back to bash-vs-sh: `sh` is the generic name for whichever POSIX sh interpreter your operating system provides; it _only_ promises features in the POSIX specification will be available (and many major operating systems, including Debian and Ubuntu, use a smaller/faster-to-startup sh interpreter like `ash` or `dash` instead of bash). `bash` is a specific interpreter with a lot of extensions. See also http://mywiki.wooledge.org/BashGuide/Practices#Choose_Your_Shell – Charles Duffy Mar 19 '22 at 15:19
  • Also, note that giving scripts a `.sh` extension is frowned on, _especially_ if they're actually bash scripts and not sh scripts. http://www.talisman.org/~erlkonig/documents/commandname-extensions-considered-harmful is an essay on the subject that the #bash IRC channel has pointed people to for years. (To pithily oversummarize: "Command names don't have extensions; you run "ls", not "ls.elf", and executable scripts are commands"). – Charles Duffy Mar 19 '22 at 15:22

1 Answers1

1

What is the difference between both the commands?

sh script.sh tells the sh program to run the script.

./script.sh says to run the script using the program that the script defines with a hashbang at the start (or in the default shell if not hashbang).

Note that you may well end up with completely different results, depending on whether the script has a hashbang, for a couple of reasons:

  • Even when sh is bash, when it's invoked as sh it runs in a limited compatibility mode (thank you Charles Duffy for pointing that out in the comments on the question).

  • The script might run with something completely different from a shell. It might start with #!/bin/node and contain JavaScript code to run in Node.js. In that case, ./script.sh will work but sh script.sh will fail, because sh will treat the script as a shell script.

Why it's not required change in permission for 'sh script.sh'?

Because in that case, what you're executing is the sh program, which is marked executable (it's sh that then reads and performs the commands in the script, but the script doesn't have to be executable for that). When you do ./script.sh, you're treating it as executable, so it needs to have the executable permissions bit set.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875