Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Given a shell script file that begins as

#!/bin/bash

# (bash script here),

and has been chmod +xed, is there any difference in running ./script and bash script from the command-line?

share|improve this question

4 Answers 4

up vote 6 down vote accepted

It depends on your $PATH. ./script will run /bin/bash script. bash script will use whatever bash comes first in your path, which isn't necessarily /bin/bash, and could be a different version of Bash.

share|improve this answer
    
Okay, that makes sense; is there a "preferred" or "recommended" way to do it? I've always seen ./script but recently saw a couple answers on Unix.SE using bash script. –  WChargin Nov 11 '13 at 21:49
1  
The more generic "bash script" version is useful if you are testing against different versions of bash. Imagine ~/bin is first in $PATH and ~/bin/bash symlinks to ~/bin/bash_versions/4.2.45/bash or ~/bin/bash_versions/4.1.11/bash or other versions. If you tried to do the same thing with /bin/bash, you would need to be root (to alter /bin) and would undoubtedly eventually break something in your distro. –  Joe Atzberger Nov 11 '13 at 22:48
2  
@WChargin People sometimes write bash script instead of ./script in usage instructions so that they don't need to explain how to give script execution permissions or what to do on the (non-Linux) systems where bash isn't in /bin. –  Gilles Nov 12 '13 at 0:10
    
@Gilles Excellent; that clears it up. Thanks. –  WChargin Nov 12 '13 at 2:35

There are two differences:

  • ./tryit.sh will not run if bash is not located in /bin, but bash tryit.sh will run if bash is anywhere on your PATH. This is because ./tryit.sh will use the shell from the bang path (/bin/bash), but bash tryit.sh will use the first bash shell on the PATH.
  • ./tryit.sh will not run if the execute bit is not set, but bash tryit.sh will run the script. You have excluded this case by specifying the bit has been set. Some edits may clear the bit causing a working command to start failing.
share|improve this answer

No they are the same. In case you had not the line #!/bin/bash or you had some other shell specified in there then ./script would use that instead of your current shell to run the script.

share|improve this answer
1  
They are different Your answer is incorrect. Marked down (sorry) –  X Tian Nov 11 '13 at 16:29

They are subtly different and easy to make a mistake.

The one you should use (which I'll write in two forms)

./script
bash ./script

My reasoning is that this is explicit, in case you've changed directory.

non explicit forms

script
bash script

Are less safe. bash script will search the current directory then the directories in the PATH environment variable, script by itself is searched only in PATH directories. Running a different command than intended could clobber your files.

share|improve this answer
3  
You seem to be answering a different question. The OP is not asking about the difference between bash script and bash script but bash ./script and ./script. If you run ./script, your $PATH is irrelevant, since you are giving a full path, the shell will never look for script in your $PATH variable. –  terdon Nov 11 '13 at 16:43
    
Upon re-reading I think you are correct, there's probably a typo in his question " is there a difference between ./script and bash script" which is what I answered –  X Tian Nov 11 '13 at 16:52
1  
@XTian No, there isn't a typo - the difference between ./script and bash script is exactly what I'm asking. However, I fail to see how you've answered this. Also, you seem to imply in your answer that simply writing script will "look in the current directory for script ... then search through the ... PATH" if it doesn't find it; unless I'm mistaken, I don't believe this is the case. Running script will only find a file on the path: $ touch script; $ script; bash: script: command not found. –  WChargin Nov 11 '13 at 21:54
    
@WChargin Ok, so there isn't a typo, my bad. I'm only trying to warn you that bash ./script is much safer than bash script. In the latter, if script is not found in your cwd, then the PATH is searched, and if found will be used to read commands from (it doesn't even have to e executable!). There's no difference between ./script vs bash ./script but a big difference when ./ is omitted. ps I sometimes come across people with dot in their PATH, you are correct, bash doesn't search for commands there normally. –  X Tian Nov 12 '13 at 13:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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