Skip to content

How to Run a Shell Script from Another Script

Advertisement

If you’re just starting with shell scripting, you might wonder how you can make one shell script run another script. This is a useful skill because it lets you reuse code and organize your tasks better. In this article, we’ll break down the process in simple steps so that even a beginner can understand.

What is a Shell Script?

Before we dive in, let’s quickly recap what a shell script is. A shell script is a file containing a list of commands that you want the computer to execute. These commands are written for the shell (the command-line interface, like Bash or Sh), and running a shell script is like asking the computer to perform several tasks at once, all listed in your script.

Why Run One Script from Another?

There are many reasons you might want to run a shell script from within another script:

  • Reusing code: Instead of writing the same code again, you can put it in one script and run it from others when needed.
  • Modularity: You can break big tasks into smaller scripts and then run them step-by-step.
  • Automation: This allows you to create a single “master” script that runs multiple scripts in order, automating a complex series of tasks.

Steps to Run a Shell Script from Another Script

Here is a step-by-step guide to doing this:

1. Create Your Shell Scripts

Let’s create two simple shell scripts as an example:


#!/bin/bash
echo "This is the first script."

For the second script:


#!/bin/bash
echo "This is the second script."

Save each script in separate files and ensure they have the .sh extension.

2. Make the Scripts Executable

Before running your scripts, you need to make them executable. To do this, use the following command in your terminal:

chmod +x script1.sh script2.sh

This command gives the scripts permission to run.

3. Run One Script from Another

To run one script from within another, simply use the script’s file path in the parent script. For example, let’s modify script1.sh to run script2.sh:


#!/bin/bash
echo "This is the first script."

# Now we will run the second script
./script2.sh

In the above code:

  • ./script2.sh runs the second script. The ./ tells the shell to look for script2.sh in the current directory.

4. Run the Parent Script

Now that you’ve set up script1.sh to run script2.sh, you can execute the parent script (script1.sh) using the following command:

./script1.sh

You should see the following output:


This is the first script.
This is the second script.

This confirms that the first script ran successfully and also called the second script, which was executed immediately afterward.

Key Points to Remember

  • Script Permissions: You must make each script executable using chmod +x.
  • Correct Paths: Ensure that the path to the script you’re calling is correct. If the script is in the same folder, use ./. If it’s in a different directory, you’ll need to specify the full path (e.g., /home/user/scripts/script2.sh).
  • Script Order: The script will call and run other scripts in the order they are written in the parent script.

Running Scripts with Arguments

Sometimes, you might want to pass information to the second script from the first. This can be done by using arguments. For example, if script2.sh is expecting an argument, like a name, you can pass it like this:


#!/bin/bash
echo "Hello, $1! This is the second script."

In script1.sh, you would write:


#!/bin/bash
echo "This is the first script."
./script2.sh Alice

In this case, when you run script1.sh, it will pass the name “Alice” to script2.sh, and you’ll see the following output:


This is the first script.
Hello, Alice! This is the second script.

Conclusion

Running one shell script from another is a simple but powerful technique that can help you automate tasks, reuse code, and keep your scripts organized. Just remember to make your scripts executable, use the correct paths to other scripts, and pass any necessary arguments if needed. With these steps, you’re ready to start chaining scripts together and improving your automation skills.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top