Running a Bash Script in Different Ways
A Bash script can be started in several ways, and they are not interchangeable. Some run the script in a new process that cannot change your shell, some run it inside your current shell, and some choose a different interpreter than the one named in the file. Knowing the differences explains many puzzling situations, such as a script that changes directory but leaves you where you were, or one that closes your terminal when it ends. This lesson uses a small script to compare every common method.
Understanding the Concept
Whenever you run a script, there are two questions to answer:
- Which interpreter reads the file? Bash,
sh, or another program. - In which process does it run? In a new child process, or in your current shell.
The second question matters most. A child process gets its own copy of the environment, so anything it changes (variables, the current directory) disappears when it ends. A script run in the current shell can change your shell, and the changes stay.
The Sample Script
Save this as info.sh in a practice directory. It prints details about itself, then sets a variable and changes directory:
#!/usr/bin/env bash
echo "script name : $0"
echo "process id : $$"
echo "directory : $(basename "$PWD")"
greeting="Hello from info.sh"
cd /
Make it executable with chmod +x info.sh. The special variable $0 is the name used to start the script, and $$ is the process ID of the shell that is running it. The last two lines set a variable and move to the root directory. They are there to show whether the changes survive.
Method 1: Pass the Script to bash
bash info.sh
Output (the process ID and directory name vary):
script name : info.sh
process id : 4821
directory : lessons
You started a new bash program and gave it the file as an argument. The shebang is ignored, because the file is data for the program you chose, and the line is only a comment. The file needs the read permission, but not the execute permission. This is the safest method to try a script you have just received.
Method 2: Run the File Directly
./info.sh
Output:
script name : ./info.sh
process id : 4830
directory : lessons
Here the system uses the shebang to pick the interpreter, as the shebang lesson explained. The file needs both the read and the execute permissions. The leading ./ is required because the current directory is not part of PATH. Note that $0 now holds ./info.sh, the path you typed.
You can also give a full path or a path relative to another directory:
/home/suriya/lessons/info.sh
../lessons/info.sh
Any command containing a slash is run from exactly that location, with no search.
Method 3: Put the Script in PATH
If you place a script in a directory listed in PATH and give it execute permission, you can run it by name alone, from any directory. This creates a small bin directory and adds it to PATH for one command:
mkdir -p bin
cp info.sh bin/info
PATH="$PWD/bin:$PATH" info
Output:
script name : /home/suriya/lessons/bin/info
process id : 4838
directory : lessons
Because Bash found the script by searching PATH, $0 is the full path where it was found. Notice that the copy has no .sh extension. Users of a command do not need to know the language it is written in, so system commands usually have no extension. To make the change permanent, add the directory to PATH in your startup file; a later lesson on startup files explains how.
Method 4: Run It with sh
sh info.sh
This starts the program named sh, which is not always Bash. On Debian and Ubuntu, sh is Dash, a smaller shell. You can check:
ls -l /bin/sh
lrwxrwxrwx 1 root root 4 Mar 31 2024 /bin/sh -> dash
Our sample script runs under Dash because it uses only simple commands. A script that uses Bash features, such as [[ ... ]], arrays or ${var^^}, would fail. Use bash, not sh, for Bash scripts.
Method 5: Feed the Script Through Standard Input
A shell reads commands from its standard input when it has no file to run. This lets you pipe or redirect a script into it:
bash < info.sh
Output:
script name : bash
process id : 4846
directory : lessons
The name is bash, because there is no script file; the commands came from the input stream. You can pass arguments to a script read from input with the -s option and --:
echo 'echo "got: $1"' | bash -s -- hello
got: hello
The -s option means "read commands from standard input", and everything after -- becomes $1, $2 and so on.
Method 6: Run a String with bash -c
For a one-line command, the -c option takes the commands as a string:
bash -c 'echo "one-liner: $((6*7))"'
one-liner: 42
The string is single-quoted so that the outer shell does not expand $((6*7)); the new bash does it. This form is how other programs, such as cron and ssh, hand a command to Bash.
Method 7: Run in the Current Shell with source or .
The previous methods all start a new process. The source command, and its shorter form, a single dot (.), do something different: they read the file and execute its lines inside your current shell.
source ./info.sh
. ./info.sh
The two lines are equivalent. The dot form is the older, POSIX name, and source is a Bash synonym. The file needs only the read permission, and the shebang line is just a comment.
The ./ in these examples only makes the location explicit. When the name has no slash, source searches the directories in PATH first and then, in Bash's default mode, the current directory, so source info.sh also finds the file in the current directory.
To see the difference clearly, run it first as a child process, and then in the current shell, checking the state of the shell after each:
./info.sh > /dev/null
echo "${greeting:-unset}"
pwd
unset
/home/suriya/lessons
The child process set greeting and changed to /, but those changes vanished with the process. The parent shell still has no greeting and has not moved. Now with source:
source ./info.sh > /dev/null
echo "${greeting:-unset}"
pwd
Hello from info.sh
/
This time both changes stayed. The shell that is running the script is your own shell, so setting a variable and calling cd changed it. That is exactly why startup files such as .bashrc and scripts that set up an environment (for example, a Python virtual environment's activate file) must be sourced and not executed: their whole purpose is to change your current shell.
The Process ID Proves It
A script printing its own process ID shows whether it runs in a new process. Save this as pid.sh:
#!/usr/bin/env bash
echo "script pid=$$ parent=$PPID"
Run it from a shell whose own ID is printed first:
echo "shell pid=$$"
./pid.sh
source ./pid.sh
shell pid=384
script pid=385 parent=384
script pid=384 parent=348
The executed script got its own process ID (385), and its parent is the shell (384). The sourced script reports the same ID as the shell (384), because it is the shell. ($$ in a script always shows the ID of the main shell process, and $PPID is the parent of that process.)
Arguments with source
A sourced file can receive arguments, which become its positional parameters while it runs:
printf 'echo "args: $# -> $1 $2"\n' > args.sh
source ./args.sh red blue
args: 2 -> red blue
exit and return in a Sourced File
Because a sourced file runs in your shell, the exit command in it exits your shell. In an interactive terminal this closes the terminal window or ends the session. To see this without risk, the test below uses a throwaway bash -c shell:
printf 'echo "about to exit"\nexit 3\n' > quit.sh
bash -c 'source ./quit.sh; echo "this line never runs"'
echo "status=$?"
about to exit
status=3
The line after source never ran, because the shell that was executing it ended. A script meant to be sourced should use return, which stops only the sourced file and gives control back to the caller:
printf 'echo "start"\nreturn 0\necho "never"\n' > ret.sh
bash -c 'source ./ret.sh; echo "back in caller, status=$?"'
start
back in caller, status=0
If the same exit 3 file is executed with ./quit.sh (after chmod +x quit.sh), only the child process ends, and the parent continues and reads the status:
./quit.sh; echo "child finished with $?"
about to exit
child finished with 3
source Needs a Real File
If the file does not exist, source reports an error and returns status 1:
bash: nosuch.sh: No such file or directory
Method 8: Replace the Shell with exec
The exec built-in replaces the current process with another program instead of starting a child. Nothing after it in the script runs:
#!/usr/bin/env bash
echo "wrapper start"
exec echo "replaced by echo"
echo "never printed"
wrapper start
replaced by echo
Use exec in a launcher script that only prepares an environment and then hands over to the real program.
Comparing the Methods
| Method | Interpreter chosen by | New process | Changes the current shell | Needs x permission |
|---|---|---|---|---|
bash script.sh | You | Yes | No | No |
./script.sh | The shebang | Yes | No | Yes |
script (found in PATH) | The shebang | Yes | No | Yes |
sh script.sh | You (sh) | Yes | No | No |
bash < script.sh | You | Yes | No | No |
source script.sh / . script.sh | Your current shell | No | Yes | No |
All of these need the read permission on the file.
What Happens When You Forget the ./
If you type only the name, Bash searches PATH and does not find the file:
info.sh
bash: info.sh: command not found
The exit status is 127. Write ./info.sh or the full path.
Practical Applications
Trying a script safely. Run an unfamiliar script with bash script.sh first. It needs no permission change, and a mistake affects only a child process.
Loading settings. Keep project settings in a file such as settings.sh, for example export API_URL="https://api.example.com", and load them with source settings.sh. The variables are then available to every command that follows in the same shell.
Reusing functions. A file that defines functions can be sourced from many scripts. Because the functions are defined in the current shell, the caller can use them afterward. A later lesson on sourcing other Bash files covers this in depth.
Running by cron or ssh. Programs that start commands for you usually go through bash -c or call the script by full path. Always give the full path in such cases, because those environments have a smaller PATH than your terminal.