2

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?

1
  • 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 Nov 2, 2015 at 19:33

3 Answers 3

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).

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.

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .