Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell: How to call one shell script from another shell script?

Tags:

bash

shell

I have two shell scripts, a.sh and b.sh.

How can I call b.sh from within the shell script a.sh?

like image 633
Praveen Avatar asked Dec 02 '11 07:12

Praveen


People also ask

What is $() in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

What is $1 and $2 in shell script?

$0 is the name of the script itself (script.sh) $1 is the first argument (filename1) $2 is the second argument (dir1)


1 Answers

There are a couple of different ways you can do this:

  1. Make the other script executable, add the #!/bin/bash line at the top, and the path where the file is to the $PATH environment variable. Then you can call it as a normal command;

  2. Or call it with the source command (alias is .), like this:

    source /path/to/script 
  3. Or use the bash command to execute it, like:

    /bin/bash /path/to/script 

The first and third approaches execute the script as another process, so variables and functions in the other script will not be accessible.
The second approach executes the script in the first script's process, and pulls in variables and functions from the other script (so they are usable from the calling script).

In the second method, if you are using exit in second script, it will exit the first script as well. Which will not happen in first and third methods.

like image 103
10 revs, 8 users 50% Avatar answered Sep 26 '22 13:09

10 revs, 8 users 50%