Difference between different ways of running shell script


Recently I have been asked a question. What are the different ways of executing shell script and what is the difference between each methods ?

I said we can run shell script in the following methods assuming test.sh is the script name,

<ol><li>sh test.sh </li> <li>./test.sh</li> <li>. ./test.sh</li> </ol>

I don't know the difference between 1 & 2. But usually in first 2 methods, upon executing, it will spawn new process and run the same. Whereas in the last method, it won't spawn new process. Instead it runs in the same one.

Can someone throw more insight on this and correct me if I am wrong?


sh test.sh

Tells the command to use sh to execute test.sh.


Tells the command to execute the script. The interpreter needs to be defined in the first line with something like #!/bin/sh or #!/bin/bash. Note (<a href="https://stackoverflow.com/questions/18331893/difference-between-different-ways-of-running-shell-script/18331933?noredirect=1#comment26906428_18331933" rel="nofollow">thanks keltar</a>) that in this case the file test.sh needs to have execution rights for the user performing this command. Otherwise it will not be executed.

In both cases, all variables used will expire after the script is executed.

. ./test.sh

Sources the code. That is, it executes it and whatever executed, variables defined, etc, will persist in the session.

For further information, you can check <a href="https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-and-sourcing-a-bash-scrip" rel="nofollow">What is the difference between executing a bash script and sourcing a bash script?</a> very good answer:


The differences are:


When you <em>execute</em> the script you are opening a <em>new</em> shell, type the commands in the new shell, copy the output back to your current shell, then close the new shell. Any changes to environment will take effect only in the new shell and will be lost once the new shell is closed.

</li> <li>

When you <em>source</em> the script you are typing the commands in your <em>current</em> shell. Any changes to the environment will take effect and stay in your current shell.

</li> </ul></blockquote>


  • WPF: CanExecute is Always Disabled
  • Should I optimize around reads or CPU time in Google App Engine
  • Cypher - matching two different possible paths and return both
  • In matplotlib, how do you change the fontsize of a single figure?
  • Cloud Code function running twice
  • Does Apple allow the usage of sysctl.h within iOS applications?
  • Creating a DropDownList
  • How to install node-mysql?
  • Azure table store snapshot/backup capability
  • Clarification on min distance on LocationManager.requestLocationUpdates method, min Distance paramet
  • Security issues with PHP's Readfile method
  • Web.config system.webserver errors
  • Tamper-proof configuration files in .NET?
  • Thread safety of a fluent like class using clone() and non final fields
  • onBackPressed() not being executed
  • Converting a WriteableBitmap image ToArray in UWP
  • Email verification using google app script and google forms
  • Meteor: Do Something On Email Verification Confirmation
  • QLineEdit password safety
  • Jenkins: How To Build multiple projects from a TFS repository?
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Is my CUDA kernel really runs on device or is being mistekenly executed by host in emulation?
  • How do I fake an specific browser client when using Java's Net library?
  • Validaiting emails with Net.Mail MailAddress
  • Perl system calls when running as another user using sudo
  • Where to put my custom functions in Wordpress?
  • Which linear programming package should I use for high numbers of constraints and “warm starts” [clo
  • Javascript + PHP Encryption with pidCrypt
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Delete MySQLi record without showing the id in the URL
  • Apache 2.4 - remove | delete | uninstall
  • 0x202A in filename: Why?
  • RestKit - RKRequestDelegate does not exist
  • Run Powershell script from inside other Powershell script with dynamic redirection to file
  • WPF Applying a trigger on binding failure
  • Free memory of cv::Mat loaded using FileStorage API
  • Angular 2 constructor injection vs direct access
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • File not found error Google Drive API
  • Programmatically clearing map cache