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?
A shell script is a text file containing a series of commands written for a shell, such as Bash, Zsh, 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:

Also check: How to Create a Shell Script in linux
Use ls to verify the script file is present in the directory:
ls

Step 2: Check File Permissions
Check if the shell script has execute permissions. Use the ls -l command:
ls -l script.sh

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

Step 4: Run the Shell Script
To run the shell script after checking all the permissions, use the command:
./script.sh

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 Message:
bash: ./script.sh: Permission denied - Solution: Use
chmodcommand 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
lscommand to ensure the file exists in the specified location.