Sometimes I have difficulty in running ./file.sh but I can run the command bash file.sh. What is the difference in the two commands? Does ./file.sh execute some other implementation of sh?
1 Answer
If "file.sh" is not executable then ./file.sh will not work but bash file.sh will.
If "file.sh" does not start with the line #!/bin/bash (or another path to a valid bash interpreter) then ./file.sh will not work but bash file.sh will.
Basically, in order for a script to look like an executable file it must:
- Have execute permission.
- Start with the line
#!/path/to/interpreter.
3 Comments
tripleee
It's also possible that the script has
#!/bin/sh but actually requires Bash in order to work correctly; then the shebang should be changed to #!/bin/bash.vkaul11
#!/bin/bash is being used
Gordon Davisson
Note that
./file.sh is generally preferred, since a script should be executable and have a proper shebang line; bash file.sh is best thought of as an override that should only be needed if there's something wrong with the script (in which case the better solution is: fix the script and use ./file.sh).
#!interpreter being used?