Difference between "./" and "sh" in UNIX

Sometimes i see that few scripts are executed through "sh" command and sometimes through "./" command.I am not able to understand the exact difference between them.Please help me out .

sh file executes a shell-script file in a new shell process.

. file executes a shell-script file in the current shell process.

./file will execute the file in the current directory. The file can be a binary executable, or it can start with a hashbang line (the first line of the file in form of #!...., for example #!/usr/bin/ruby in a file would signify the script needs to be executed as a Ruby file). The file needs to have the executable flag set.


For example, if you have the script test.sh:

#!/bin/sh

TEST=present

and you execute it with sh test.sh, you'd launch a new sh (or rather bash, most likely, as one is softlinked to the other in modern systems), then define a new variable inside it, then exit. A subsequent echo $TEST prints an empty line - the variable is not set in the outer shell.

If you launch it using . test.sh, you'd execute the script using the current shell. The result of echo $TEST would print present.

If you launch it using ./test.sh, the first line #!/bin/sh would be detected, then it would be exactly as if you wrote /bin/sh ./test.sh, which in this case boils down to the first scenario. But if the hashbang line was, for example, #!/usr/bin/perl -w, the file would have been executed with /usr/bin/perl -w ./test.sh.

What's the difference between a cold, the flu, seasonal allergies, and , Learn the differences between colds and flu - CDC. have similar symptoms, it can be difficult to tell the difference between them based on symptoms alone. Difference Between has a large knowledge base and deal with differences between popular terms, technologies, things and anything to everything.

In simple words, sh file1 executing sh command/executable with file1 as a parameter. In this case file1 doesn't require execute privilege as sh executable read and intercept the commands in the file.

./file1 its nothing but running/executing an executable file file1, hence it requires executable privileges. In this case it executes on the shell mentioned in the shebang #!/bin/sh if its not mentioned then its on the current shell.

Hoping the above statements are not chaos :)

Cold Versus Flu, What is the difference between Alzheimer's and dementia? Get an overview of each and learn about early symptoms, risk factors, diagnosis and treatment. The key difference between archenteron and blastocoel is that archenteron is the primary gut formed during gastrulation in the developing zygote, which later develops into the digestive tube, while blastocoel is an inner fluid-filled or yolk-filled cavity of the blastula formed during blastulation. Fertilization is the event of sexual

With sh , we can run a script that doesn’t have execute permission set on it, we run it as argument for sh, but ./ needs the permission as it is supposed to be an executable. In both cases, new shell will be created to run the script. See the below example:

root@ub18:~/shell# vi test1.sh
#!/bin/bash
my_var=hello
echo $my_var
#Shows the current shell processid
echo $$
root@ub18:~/shell# echo $$
1896
root@ub18:~/shell# sh test1.sh
hello
2093
root@ub18:~/shell# ./test1.sh
-su: ./test1.sh: Permission denied
root@ub18:~/shell# chmod +x ./test1.sh
root@ub18:~/shell# ./test1.sh
hello
2102
root@ub18:~/shell# ./test1.sh
hello
2103
root@ub18:~/shell# ./test1.sh
hello
2104
root@ub18:~/shell# sh test1.sh
hello
2106

Dementia vs. Alzheimer's Disease: What Is the Difference?, Learn about the differences between COVID-19 (Coronavirus) tests: Molecular (​Swab) Test, Antibody (Serology) Test, Antibody Tests for IgG and Antibody Test  Define vs. vs synonyms, vs pronunciation, vs translation, English dictionary definition of vs. abbr. veterinary surgeon abbreviation for versus abbreviation for

when your file is not executable you can't run using ./file_name.sh normally you can run by sh file_name.sh if you change your file to executable chmod a+x file_name.sh you can run your file by ./file_name.sh

The Difference between Tests for COVID-19 (Coronavirus), The difference between quarantine and isolation, in public health, comes down to whether someone has been exposed or infected with a communicable  What's the difference between a single and a double quotation mark in English? I've heard that it only depends on where you live the US (for double quotation mark) or the UK and Australia (for single

What Is The Difference Between “Quarantine” And “Isolation , What is the difference between isolation and quarantine? Isolation and quarantine are public health practices used to protect the public by preventing exposure  Which vs. That: What’s the Difference in Usage? To understand when to use that or which, it’s important to understand clauses.A defining clause (also called an essential clause or a restrictive clause) gives information essential to the meaning of the sentence.

What is the difference between isolation and quarantine?, Not regulated and do not protect the wearer. Not to be used by people who are sick. Breathing into a facemask builds up moisture, which can. The Difference Between .com, .net, .org and Why We’re About To See Many More Top-Level Domains. Chris Hoffman @chrisbhoffman October 15, 2012, 6:00am EDT

[PDF] The Difference Between Alternative Masks and Surgical Masks for , What's the difference? Bacteria and viruses can cause many common infections. But what are the differences between these two kinds of  The world's best bras. The sexiest panties & lingerie. The most beautiful Supermodels. Discover what's hot now - from sleepwear and sportswear to beauty products.

Comments
  • maybe also interesting: askubuntu.com/q/22910/354350
  • Thanks a lot.Although,i was able to run the .sh file through ./ without mentioning the hashbang line.How come it was possible ?
  • Without a hashbang line, bash will assume it is a shell script file. You do need a hashbang if you want to use any other interpreter.
  • most useful answer I've found