How to Run a Script in Linux?

Topics Covered

Overview

Running scripts is a fundamental aspect of working with the Linux operating system. Scripts allow you to automate repetitive tasks, execute a series of commands, or perform complex operations with ease. Whether you are a system administrator, developer, or Linux enthusiast, understanding how to run scripts is essential to maximize productivity and efficiency.

This article aims to provide comprehensive details about how to run a script in Linux. We will explore the various ways to execute scripts, from simple one-liners to more elaborate shell scripts. Additionally, we will cover important concepts such as file permissions, shebangs, and environment variables that play a crucial role in running scripts successfully.

Pre-requisites

Before running a script in Linux, there are a few prerequisites you should ensure:

Script file: You need to have a script file that contains the code you want to run. The script file should have the appropriate file extension depending on the scripting language you're using. For example, a shell script typically has a .sh extension.

Terminal or command-line interface: You'll need access to a terminal or command-line interface on your Linux system. This allows you to interact with the command line and execute commands.

Text editor: It's helpful to have a text editor to create and modify your script files. Linux systems usually come with built-in text editors like Nano or Vim, or you can install other popular editors like Emacs or Sublime Text.

Correct shebang line: For shell scripts, ensure that the script file starts with the appropriate shebang line. The shebang line specifies the interpreter to be used to run the script. For example, #!/bin/bash indicates that the script should be run using the Bash shell.

Execution permissions: If the script file is not located in a directory listed in the system's PATH variable, you need to provide execution permissions to the script file. This can be done using the chmod command.

By fulfilling these prerequisites, you'll be ready to run your script in Linux.

Using bash/sh

To run a script in Linux using the bash or sh command, follow these steps:

Step 1: Open a terminal or command-line interface.

Step 2: Navigate to the directory where your script is located using the cd command. For example, if your script is in the "home" folder, you can use: cd ~/home.

Step 3: Execute the script by typing bash hello.sh or sh hello.sh, replacing "hello" with the actual name of your script.

Step 4: Press Enter to run the script.

Here's an example:

script

configuration of script

execution of script

This will run the script named "hello.sh" located in the "home" folder.

Note that both the bash and sh commands can be used to execute the script. However, there might be slight differences in behavior depending on the default shell assigned to the user. It's recommended to use bash for running bash scripts specifically.

And also Ensure that the script file has the appropriate shebang line at the beginning to specify the interpreter, such as #!/bin/bash for a bash script.

By following these steps, you can run a script in Linux using the bash or sh command.

Using Source to Run a Script in Linux

To run a script in Linux using the source command, follow these steps:

Step 1: Open a terminal or command-line interface.

Step 2: Navigate to the directory where your script is located using the cd command. For example, if your script is in the "home" folder, you can use: cd ~/home.

Step 3: Use the source command or its shorthand ./ followed by the script name. For example, source hello.sh or ./hello.sh, replacing "hello" with the actual name of your script.

Step 4: Press Enter to run the script.

Here's an example:

or

execution of script

This will run the script named hello.sh located in the "home" folder.

Using the source command or ./ executes the script within the current shell session. This allows the script to modify the environment variables or settings of the current session, which can be useful for scripts that need to make changes to the current shell environment.

Note that when using source or ./, you don't need to provide execution permission to the script file. However, ensure that the script file has the appropriate shebang line at the beginning to specify the interpreter, such as #!/bin/bash for a Bash script.

By following these steps, you can run a script in Linux using the source command or its shorthand ./.

Using Chmod and Specifying the Path to the Script

To run a script in Linux using the chmod command and specifying the path to the script, follow these steps:

Step 1: Open a terminal or command-line interface.

Step 2: Navigate to the directory where your script is located using the cd command. For example, if your script is in the "home" folder, you can use: cd ~/home.

Step 3: Make the script executable by running the chmod +x home.sh command, replacing "home" with the actual name of your script. This command grants execution permissions to the script file.

Step 4: Execute the script by typing ./home.sh, replacing "hello" with the actual name of your script, and prepend it with ./ to specify the current directory.

Step 5: Press Enter to run the script.

Here's an example:

This will run the script named hello.sh located in the "home" folder.

By using chmod +x, you provide the script file with the necessary permissions to be executed as a standalone program. The +x option adds the execution permission to the file.

Ensure that the script file has the appropriate shebang line at the beginning to specify the interpreter, such as #!/bin/bash for a bash script.

By following these steps, you can run a script in Linux by using the chmod command to make it executable and specifying the path to the script using ./.

Conclusion

  • This article is a comprehensive introduction to how to run a script in Linux.

  • Running a script in Linux involves using a terminal or command-line interface.

  • You can use either the bash or sh command to execute the script.

  • Alternatively, you can use the source command or its shorthand . to run the script.

  • If necessary, you can make the script executable using the chmod +x command.

  • Specifying the path to the script is essential for running it using the ./ notation.

  • Choose the method that suits your needs and the requirements of your script.