0

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
  • What is the #! interpreter being used? Commented Aug 5, 2013 at 21:51

1 Answer 1

3

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:

  1. Have execute permission.
  2. Start with the line #!/path/to/interpreter.
Sign up to request clarification or add additional context in comments.

3 Comments

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.
#!/bin/bash is being used
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).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.