how to call shell script (.sh) file from another shell script

When writing or developing shell scripts in Linux, you might want to call or execute another script file from with in the current script. There are three different ways you can do this, and which method you want to use will depend on what your requirements are.

Firstly, you need to know how exactly you want the other script to run? The two ways you can execute the script and how you do it may have an impact on the result.

Do you want the included script to run in a different process?

Running the script in a different process is like executing it from the command line. It has access to the environment variables that are set in the system or shell. The system spawns a new process and executes the script in that process. This is appropriate if there is no variable values that it needs from the caller script. This is also the best option, if you do not want to pollute the current shell with any values from the callee.

For example, you don’t want the callee to change the current working directory and the caller make a safe assumption about which working directory it is in after the script returns.

Do you want the script to run in the same process as the caller?

Sometimes, the caller script (or parent) had already made some modifications to the environment and you want the callee to be able get those values. Also, the called script might modify some values that you want propagated back to the shell so that the new values can be used the parent.

This is also useful when you want to call specific functions with in the sourced or other script.

 

We are going to assume that you have the correct permissions to execute the scripts and that they are in the correct path. We will also assume that both the script are coded to be called independently, meaning it has the shebang and executing permissions for the shell.

Running in a different process

Call the script as you would call from the command line. I recommend using the absolute path to the script, as it allows it to be maintainable and independent of the PATH variable. Also, it will fail when the called script is moved rather than accidentally picking up another by the same name.

#!/bin/bash
# call the other script using absolute path
/path/to/the/theotherscript.sh argument1
# using the relative path (in the same folder)
./theotherscript.sh

You can also specify the shell when calling the other script just as you would from the command line.

#!/bin/bash
# call the other script using absolute path
/bin/sh /path/to/the/otherscript.sh

Running in the same process

When you want the script to execute in the same process context, you use the source command (if in bash). You can use the dot operator as well. The source command is just an alias for the dot operator in bash. Both will pretty much work the same in a bash shell context.

#!/bin/bash
# call the other script using absolute path, using source
source /path/to/the/theotherscript.sh argument1
# using the relative path (in the same folder) using the dot (.) operator
. ./theotherscript.sh

Using exec command

There is yet another option to execute or call another script. You can use the exec command to execute the script. This works slightly different than the previous commands. I mostly try to stay away from it, because I have had some bad experiences with it.

The exec will kill or terminate the current shell before executing the called script. So, you will end up with the context of the called script rather than the caller script when the script ends. In most cases, it is fine and work just as well.

It is not advisable if there are more commands to be executed in parent script after the called script finishes. If the called script is last command in the script, it will work.

#!/bin/bash
# call the other script using absolute path
exec /path/to/the/theotherscript.sh argument1
echo "I will never print this text"

I will recommend using the first method of calling the script in a separate process in most cases, unless you have the specific requirement of having to execute in the same context. I would advise strongly against using the exec command, unless you really know what you are doing.