Uncategorized

How to Run a Shell Script in Linux

From https://www.geeksforgeeks.org/linux-unix/how-to-run-shell-script-in-linux/ (23/11/2025)

How to Execute Shell Script in Linux?

shell script is a text file containing a series of commands written for a shell, such as BashZsh, or Sh, to execute. It automates repetitive tasks, simplifies complex operations, and can include programming constructs like loops, conditionals, and variables.

They typically have a .sh extension and can be executed directly if given the proper permissions.

To execute a Shell Script, you have to create it with the use of a text editor like nano or vi.

Step 1: Navigate to the Script’s Directory

Now, in your terminal, use the cd command to navigate to the directory where your script is located.

cd /path/to/your/script

For example:

Screenshot-from-2024-12-05-15-11-45
Path to execute shell script

Also check: How to Create a Shell Script in linux

Use ls to verify the script file is present in the directory:

ls
Screenshot-from-2024-12-05-15-12-49
verify script file

Step 2: Check File Permissions

Check if the shell script has execute permissions. Use the ls -l command:

ls -l script.sh
Screenshot-from-2024-12-05-15-14-18
check shell script file permissions

If you see -rw- at the start of the permissions, the script is not executable.

Step 3: Grant Permission to Execute Shell Script

If needed, use the chmod command to make the script executable:

chmod +x script.sh
Screenshot-from-2024-12-05-15-21-09
granting permissons to execute shel script file

Step 4: Run the Shell Script

To run the shell script after checking all the permissions, use the command:

./script.sh
Screenshot-from-2024-12-05-15-23-00
running the shell script

As you can see, once the script gets executed, it’s output: Hello, World is shown in the terminal.

Common Errors in Running Shell Script

When running shell scripts in Linux, you may encounter several common errors. Here’s a list of these issues along with explanations and solutions:

1. Permission Denied

  • Cause: The script does not have execute permissions.
  • Error Messagebash: ./script.sh: Permission denied
  • Solution: Use chmod command to grant execute permissions:
chmod +x script.sh

Then, try running the script again:

./script.sh

2. Command Not Found

  • Cause: A command in the script is either misspelled or not installed on the system.
  • Error Message./script.sh: line X: command: not found
  • Solution: Check the script for typos or invalid commands and install the missing command using your package manager. For example:
sudo apt install <command-name>

3. File Not Found

  • Cause: The script is trying to access a file that doesn’t exist or is in a different location.
  • Error Message./script.sh: line X: /path/to/file: No such file or directory
  • Solution: Verify the file path in the script, use the ls command to ensure the file exists in the specified location.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.