1

When executing a bash script in the Linux Terminal, is there a difference between "bash foo.sh" and "./foo.sh"? Or are they just two different ways of performing the same function?

  • 2
    Assuming the shebang line for foo.sh is pointing to bash they'd be the same thing, oh, and assuming you have the execute permission on foo.sh which is not required for bash foo.sh but is for ./foo.sh – Eric Renouf Nov 2 '15 at 19:33
1

They are likely equivalent if ./foo.sh

  1. is executable, and
  2. begins with with #!/bin/bash (or equivalent)

Otherwise, and given the precondition you describe (foo.sh is a bash script), the ./foo.sh version may well fail, either because it doesn't have execute permission for the user, or because it is interpreted by /bin/sh instead of /bin/bash (/bin/sh is the default script interpreter).

| improve this answer | |
0

In addition to Tobys answer I want to point out that one should check existing shebang bash params in the script (like #!/bin/bash -e) before calling just 'bash foo.sh'. Calling a script via 'bash foo.sh' will not care about existing bash params in the shebang.

| improve this answer | |
0

People already explained here but in simple words bash foo.sh mean foo.sh will be executed in bash shell.

It is just a way to tell the kernel to which shell script will be use to run the script with making any changes in script.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.