How to Run a Shell Script from Bash Script

Running a shell script in Bash is one of the most fundamental and useful skills for anyone working with Linux systems. Shell scripts allow you to automate repetitive tasks, manage system configurations, and create complex workflows in a few lines of code. Whether you’re a beginner or an experienced user, mastering this will save you time and effort. In this article, we will walk through each step to successfully run a shell script in Bash, providing detailed instructions to ensure your understanding.

In this tutorial you will learn:

  • How to create a shell script in Bash
  • How to execute a shell script
  • How to manage permissions for running a script
  • How to run one script from another in Bash
How to Run a Shell Script from Bash Script
How to Run a Shell Script from Bash Script
Software Requirements and Linux Command Line Conventions
Category Requirements, Conventions or Software Version Used
System A Linux-based operating system
Software Bash shell (most Linux distributions include Bash by default)
Other Text editor for writing the script (e.g., nano, vim, or any IDE)
Conventions # – requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
$ – requires given linux commands to be executed as a regular non-privileged user

Steps to Run a Shell Script in Bash

To run a shell script in Bash, you need to create the script, assign executable permissions, and finally execute the script in your terminal. Let’s go step by step.

  1. Create a Shell Script:
    The first step in running a shell script in Bash is to create the actual bash script file. A shell script is essentially a plain text file containing a series of commands that are executed in sequence. You can create this file using a text editor such as nano or vim.

    $ nano myscript.sh

    After opening the text editor, you can write your desired commands in the script. For example:

    #!/bin/bash
    echo "Hello, World!"

    The #!/bin/bash line at the top of the file tells the operating system that this script should be run using the Bash shell. The echo command prints text to the screen.

    Once you’re done writing the script, save and exit the text editor. If you’re using nano, press CTRL+X, then Y to confirm the save, and hit Enter to exit.

  2. Make the Script Executable:
    Before you can run the script, you need to make it executable. This ensures that the system recognizes it as a program that can be executed. Use the chmod command to modify the script’s permissions.

    $ chmod +x myscript.sh

    This command grants execute permissions to the script, allowing it to be run by the system.

  3. Run the Shell Script:
    After making the script executable, you can now run it. To do so, navigate to the directory where your script is located and execute it by typing:

    $ ./myscript.sh

    The ./ tells the terminal to look for the script in the current directory. The script will run, and you should see the output Hello, World! on your terminal.

  4. Run a Script from Another Script in Bash:
    Sometimes, you may need to run one script from within another script in Bash. This can be done by either providing the full path or using a relative path, depending on your requirements.

    • Using a Relative Path: A relative path refers to the location of the script in relation to the current working directory (the directory from which you are running the script). Here’s how you can run another script using a relative path:
      #!/bin/bash
      ./myscript.sh
      echo "Main script executed!"

      In this example, ./myscript.sh refers to a script that is in the same directory as the main script. The ./ denotes the current directory, so Bash knows to look for myscript.sh in the same folder.

    • Using a Full Path: A full or absolute path specifies the exact location of the script, starting from the root directory. This method is useful if the script you want to execute is located in a different directory or if you want to ensure the script is run regardless of your current directory.
      #!/bin/bash
      /home/user/scripts/myscript.sh
      echo "Main script executed!"

      In this case, /home/user/scripts/myscript.sh refers to the exact location of the script. Regardless of where the main script is located, it will always run the another_script.sh located in /home/user/scripts/.

    Difference Between Relative and Full Path:

    • Relative Path: Flexible, but dependent on where the main script is run from. It only works if the two scripts are in the same directory or in a predictable relative location.
    • Full Path: Ensures that the correct script is always run, regardless of where the main script is executed from. It is ideal when the scripts are stored in different directories or when you want more control over execution.

    By choosing the appropriate method, you can easily manage script execution across different directories or environments.

  5. Run a Script Using Bash Command:
    Alternatively, you can run a shell script using the bash command without making it executable. This is useful if you don’t want to change file permissions.

    $ bash myscript.sh

    This command runs the script directly using the Bash interpreter, bypassing the need for executable permissions.

Conclusion

By following these steps, you can easily create, execute, and manage shell scripts in Bash. Whether you’re automating tasks, managing files, or running scripts from other scripts, mastering this skill will significantly enhance your productivity and efficiency on a Linux system. Understanding how to run a shell script in Bash opens up endless possibilities for automation and system management.

FAQ:

1. How do I make a shell script executable?

To make a shell script executable, use the following command:

$ chmod +x script_name.sh

This grants execute permissions, allowing the script to run.

2. What is the common error: “Permission denied” when running a script?

This error occurs when the script does not have executable permissions. Use the chmod +x command to make the script executable:

$ chmod +x script_name.sh

3. How do I run a script in Bash without making it executable?

You can run the script directly using the Bash command:

$ bash script_name.sh

4. What does “No such file or directory” mean?

This error typically means that the script cannot be found in the specified path. Ensure you are in the correct directory or use the full path to the script:

$ ./script_name.sh

for relative paths, or use the full path like:

$ /home/user/script_name.sh

5. How can I run a script from another script in Bash?

You can run another script by calling it within the main script using either a relative or full path:

./another_script.sh

(relative) or

/home/user/another_script.sh

(full path).

6. What does “command not found” mean when executing a script?

This error indicates that Bash cannot locate the script in your current directory. Make sure to include ./ before the script name if the script is in your current directory:

$ ./script_name.sh

7. Can I run a script as a root user?

Yes, you can run a script with root privileges by using sudo:

$ sudo ./script_name.sh

This is required if the script needs to modify system files or perform administrative tasks.

8. How do I debug a Bash script?

To debug a Bash script, you can run it with the -x option:

$ bash -x script_name.sh

This will print each command before it’s executed, helping you trace errors or unexpected behavior.



Comments and Discussions
Linux Forum