Frank
Frank

Reputation: 7575

what's the difference between ./script.sh and bash script.sh

On linux system, is there any difference to kick of script.sh in these two different ways?

Are they exactly the same thing?

Thanks

Upvotes: 2

Views: 1308

Answers (5)

V H
V H

Reputation: 8587

script.sh can be called script.anything as pointed on on the knittl post within bully, the script is identified by

#!/whatever/it/is

running bash ./script.something means your telling it to execute the script using bash and this can return errors rather than run also bully forgot

#!/usr/bin/perl

so you could have a perl script called script.sh

 cat perl.sh 
#!/usr/bin/perl

print "Hello World\n";
:~/Documents$ ./perl.sh 
Hello World

~/Documents$ bash ./perl.sh 
Warning: unknown mime-type for "Hello World\n" -- using "application/octet-stream"
Error: no such file "Hello World\n"

Upvotes: 0

Fredrik Pihl
Fredrik Pihl

Reputation: 45634

In order for the first form to work, the file must have the executable bit set, secondly it needs to have a shebang which specifies which interpreter that will be used.

So yes, both forms are identical (when it comes to what will be interpreted).

For a history-lesson see this

Current implementation of the she-bang parsing in the linux-kernel can be found here

Upvotes: 1

bully
bully

Reputation: 5994

I don't think this is exactly the same.

As far as I understand, you simply execute a script with ./script.sh on the current shell. It hasn't to be a bash (Bourne Again SHell) you're running, it can be any shell installed on your system.

If you execute a script with bash script.sh, you tell the system that you want the script to be executed explicitly with a bash shell.

You can see which shells are available for your system by calling:

$ cat /etc/shells 
# /etc/shells: valid login shells
/bin/sh
/bin/dash
/bin/bash
/bin/rbash

Upvotes: 0

Hernan Velasquez
Hernan Velasquez

Reputation: 2820

Is not necessarily the same.

If you run script.sh linux will search on the directories set in the $PATH env variable.

With ./script.sh, linux will run the script located on the directory where you are at the moment of the call.

Upvotes: -2

Desislav Kamenov
Desislav Kamenov

Reputation: 1203

./script.sh expects that this file is in the current directory, has execute bit set and the first line of the file is path to the interpreter to start with ( Shebang line )

bash script.sh means that you invoke bash and pass the contents of the file to be executed(interpreted) as bash commands. This way your file doesn't need to be executable and has a shebang line.

If the conditions for ./script.sh are met then both invocations lead to the same result.

Upvotes: 7

Related Questions