What is the difference between using `sh` and `source`?

What is the difference between sh and source?

source: source filename [arguments]
    Read and execute commands from FILENAME and return.  The pathnames
    in $PATH are used to find the directory containing FILENAME.  If any
    ARGUMENTS are supplied, they become the positional parameters when
    FILENAME is executed.

And for man sh:

       bash - GNU Bourne-Again SHell

       bash [options] [file]

       Bash is Copyright (C) 1989-2004 by the Free Software Foundation, Inc.

       Bash  is  an sh-compatible command language interpreter that executes commands read from the standard input or from a file.  Bash also incorporates
       useful features from the Korn and C shells (ksh and csh).

       Bash is intended to be a conformant implementation of the IEEE POSIX Shell and Tools specification (IEEE Working Group 1003.2).

What are the differences between "sh file.sh" and "source file.sh , source foo.sh runs the script in the current Bash shell, very similar to simply replacing the source foo.sh command by the contents of the foo.sh file. sh foo.sh runs the script within the context of a new sh shell, which on many OSes is not even Bash (which may be one reason the script doesn't work). The Bourne shell (sh) is a shell, or command-line interpreter, for computer operating systems.. The Bourne shell was the default shell for Version 7 Unix. Unix-like systems continue to have /bin/sh—which will be the Bourne shell, or a symbolic link or hard link to a compatible shell—even when other shells are used by most users.

The main difference is that they are executed in a different process.

So if you source a file foo which does a cd, the sourcing shell (e.g. your interactive shell in the terminal) is affected (and its current directory will change)

If you execute sh foo the cd does not affect the sourcing shell, only the freshly created sh process running foo

Read the Advanced Bash Scripting Guide.

That difference is not specific to Linux; every Posix implementation would have it.

'source' vs 'sh', 'source' vs 'sh' | The Difference Between Sourcing and Running a Script This is in contrast to running a script with sh , in which case the environment variables� source filename [arguments] source functions.sh source /path/to/functions.sh arg1 arg2 source functions.sh WWWROOT=/apache.jail PHPROOT=/fastcgi.php_jail Example. Create a shell script called mylib.sh as follows:

As others have mentioned, when you run sh test.sh, any changes that test.sh makes to your shell environment won't persist after the process has ended.

However, also note that any element of your environment that isn't exported (e.g., variables, aliases, and shell functions) won't be available to the code in test.sh when it is executed as a subprocess (i.e. with sh test.sh).

For example:

$ cat > test.sh
echo $foo
$ foo=bar
$ sh test.sh
$ . test.sh

Example 2:

lap@my-ThinkPad:~$ cat test.sh
cd /etc
lap@my-ThinkPad:~$ sh test.sh 
lap@my-ThinkPad:~$ pwd
lap@my-ThinkPad:~$ source test.sh 
lap@my-ThinkPad:/etc$ pwd

command line, When you run any script by passing the filename to the script interpreter program, you are running the interpreter program with the script as an� sh == bash? For a long time, /bin/sh used to point to /bin/bash on most GNU/Linux systems. As a result, it had almost become safe to ignore the difference between the two. But that started to change recently. Some popular examples of systems where /bin/sh does not point to /bin/bash (and on some of which /bin/bash may not even exist) are:

source (or . ) - runs inside current shell and changes its attribute/environment.

sh do fork and runs in a subshell and hence can't change attributes/environment.

For example

My shell script is -

elite12!rg6655:~/sh_pr [33]$ cat changeDir.sh
cd /home/elt/rg6655/sh_pr/justdir
echo $$

My Current Shell -

elite12!rg6655:~/sh_pr [32]$ echo $$

Process id of my current shell is 3272

Running with the source -

elite12!rg6655:~/sh_pr [34]$ source changeDir.sh

Observer two things - 1) The process id (3272) is same as my shell, which confirms source executes in the current shell. 2) cd command worked and directory got changed to justdir.

Running with sh -

elite12!rg6655:~/sh_pr [31]$ sh changeDir.sh

In this case, process id (13673) is different and directory remains the same which means it is running in a different process or subshell.

What is the difference between "." "./" and "source"?, source script. This also sources the script. The source command is not required by POSIX and therefore is less portable than the And is usually used with an slash after it: ls -d ./ In the shell, a dot also means: "source a file". This article attempts to differentiate between source and resource, to make readers use the right word in the right context. Source Milk is a source of protein that we need to drink to fulfill our daily requirements of protein.

source or dot operator Man Page - Linux, in bash, but not in POSIX sh, so for maximum compatibility use the period. There is a subtle difference between executing a script by running .ss64script ( dot� Source the script: $ source pid.sh 25009 Execute the script, note the PID: $ ./pid.sh 25011 Source again: $ source pid.sh 25009 Execute again: $ ./pid.sh 25013 You can see that sourcing the script runs in the same process while executing the script creates a new process everytime.

Bash vs Zsh: A comparison of two command line shells (2019 Update), With the release of Version 7 Unix in 1979, the new OS came distributed with the Bourne shell as its default shell. Creation of bash. Later in 1989,� Southern Hitters vs Western Warriors Dream11 Prediction: Best picks for SH vs WW in Malaysian T10 Bash - SH vs WW Dream11 Team - Check My Dream11 Team, Best players list of today's match, Southern Hitters vs Western Warriors Dream11 Team Player List, SH Dream11 Team Player List, WW Dream11 Team Player List, Dream11 Guru Tips, Online Cricket Tips, Southern Hitters vs Western Warriors Head to Head.

The Difference Between Bash and Powershell, Writing shell scripts is like building something with Lego (or your preferred brand of architectural, interlocking brick system), one brick is�

Bash Source Command, Source is a shell built-in in Bash and other popular shells used in Linux and Its behavior may be slightly different from shell to shell. In this section, we will look at some basic examples of how to use the source command.

  • Should also note that any environment variables that aren't exported won't be available to a script that is invoked under a new shell.
  • (The same goes for aliases and shell functions.)
  • @WillVousden: Alias seem not to be exportable at all, you could export variables and/or functions, but not aliases.
  • Yep, I don't think aliases can be exported.
  • No, do not read the Advanced Bash (in fact they mean Bug) Scripting Guide
  • Why do you think the ABSG is wrong for newbies? It teaches a lot of useful things.... What alternative guide do you suggest?
  • It teaches the worse bash practices. Just look at the first examples, you'll understand how terrible it is: uppercase variable names, useless uses of cats, ...
  • Then suggest an alternative better tutorial document. I think it is good enough for newbies...
  • One alternative is mywiki.wooledge.org/BashGuide and there are links to more at wiki.bash-hackers.org/scripting/tutoriallist
  • You don't need execute permission to invoke by passing it as an argument to sh (e.g. sh test.sh). You only need it if you want to invoke it directly (e.g. ./test.sh).