How to Run a Shell Script in Linux
Last Updated :
24 Dec, 2024
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently.
In this guide, we'll show the steps to check a shell script in Linux before running it, ensuring it is error-free and performs as expected. From verifying script syntax to understanding execution permissions, we'll show everything you need to run your scripts.
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.
Here is: How to Create a Shell Script in linux
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:
Path to execute shell scriptAlso check: How to Create a Shell Script in linux
Use ls to verify the script file is present in the directory:
ls
verify script fileStep 2: Check File Permissions
Check if the shell script has execute permissions. Use the ls -l command:
ls -l script.sh
check shell script file permissionsIf 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
granting permissons to execute shel script fileStep 4: Run the Shell Script
To run the shell script after checking all the permissions, use the command:
./script.sh
running the shell scriptAs 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
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.
Also Read:
Conclusion
Executing shell scripts in Linux is an essential skill for automating tasks and streamlining system processes. By following the steps outlined in this guide—such as creating a script, checking permissions, and resolving common errors—you can run shell scripts efficiently and with confidence. Understanding the basics of script creation and execution not only saves time but also ensures your scripts perform as intended.
Whether you're a beginner or an experienced user, mastering shell scripts opens up endless possibilities for managing tasks, handling files, and simplifying complex operations. As you practice and experiment, you'll uncover the true power of Linux scripting.
How to Run Shell Script in Linux - FAQs
How can I run shell script in Linux?
Steps to Run a Shell Script in Linux:
Navigate to Script's Directory: Use cd /path/to/your/script.
Check Permissions: Run ls -l script.sh to see permissions.
Grant Execute Permission (if needed): Use chmod +x script.sh.
Execute the Script: Run the script with ./script.sh or bash script.sh.
How to make .sh file executable?
Make a Bash Script Executable
1) Create a new text file with a . sh extension
2) Add #!/bin/bash to the top of it. This is necessary for the “make it executable” part.
3) Add lines that you'd normally type at the command line
4) At the command line, run chmod u+x YourScriptFileName.sh.
How to run a file on terminal?
Steps to Run a File on Terminal:
Open Terminal: Launch the terminal on your system.
Navigate to the File's Directory: Use cd /path/to/your/file.
Check File Type: Use file filename to determine the type (e.g., script, binary, etc.)
Set Permissions (if needed): For executable files, use chmod +x filename.
Run the File: For scripts, use ./filename or bash filename. For programs, just type ./filename.
How to run a sh file with sudo?
To run the command sudo sh /path/to/script.sh , you need to go to a terminal, type sudo sh /path/to/script.sh , and then enter the password.
Similar Reads
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
How To Run Bash Script In Linux?
Bash scripts, also known as shell scripts, are powerful tools in the world of command-line automation. They allow you to perform a series of tasks or execute commands by writing scripts that can be run in a terminal or command-line interface. However, the question often arises: how do you run a Bash
6 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Enable Shell Script Debugging Mode in Linux?
Linux is a very popular operating system especially among developers and in the computer science world, as it comes along with its powerful ability to play with the kernel. Major Linux distributions (often referred to as Linux distros) are Open Source (Source Code available and can be modified on re
5 min read
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether it’s a script, a compiled program, or even a text file. In this article,
6 min read
How to Run a File in Linux
The command line is one of the most powerful tools in Linux. It allows you to execute commands, manage files, and automate tasks all from a single terminal window. One common task you'll often need to do is run a file, whether it’s a script, a compiled program, or even a text file. In this article,
6 min read
How to prompt for Yes/No/Cancel input in a Linux shell script
You may have noticed that shell programs occasionally ask the user for confirmation by prompting [Y/n] or [Yes/No]. Knowing whether a user wishes to continue with the following stages or not is useful. A similar feature can be added to your script as well. This article will assist you with examples
3 min read
Introduction to Linux Shell and Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by using the terminal. In this article we will discuss Linux shells and shell scripting so before understandi
7 min read
How to Install Z Shell(zsh) on Linux?
The ZSH or Z Shell is a Unix based shell or can be called as a command-line interpreter. It is an extended version of the good old Bourne shell (sh). It does have some features form Bash with lots of added features like automatic cd (Change Directory), spell check, path expansion, and many more. It
2 min read
How to Open Terminal in Linux?
The Shell or Command Prompt are common name for the Linux Terminal. The terminal was designed as a Linux File Browser, and this function is still carried out using it. You may traverse your files and undo changes made by using the Terminal as a File Browser. However, the very first step will be to O
4 min read
How To Change Default Shell In Linux
In most Linux systems, the default shell is bash but we can change that to any other shell-like zsh, fish, sh, and any other. In this article, we are going to show how to change that default shell to any other shell in Linux systems. To change the user's shell, first, let's find the current shell. T
4 min read
How To Change Default Shell In Linux
In most Linux systems, the default shell is bash but we can change that to any other shell-like zsh, fish, sh, and any other. In this article, we are going to show how to change that default shell to any other shell in Linux systems. To change the user's shell, first, let's find the current shell. T
4 min read
How to Open a File in Linux
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everything—files, directories, devices, and processes—as a file. Whether you're accessing a simple text docu
6 min read
How to Open a File in Linux
In Linux, a file is a fundamental unit of storage, representing everything from documents and images to system logs and program data. Unlike traditional operating systems, Linux treats almost everything—files, directories, devices, and processes—as a file. Whether you're accessing a simple text docu
6 min read
How to Set File Permissions in Linux
Setting up the file permission is a very crucial task as it ensures that only authorized users can read, write, or execute files, enhancing the security and functionality of your system. In this guide, we will explain how to set file permissions in Linux using various commands and techniques. Whethe
9 min read
How to Set File Permissions in Linux
Setting up the file permission is a very crucial task as it ensures that only authorized users can read, write, or execute files, enhancing the security and functionality of your system. In this guide, we will explain how to set file permissions in Linux using various commands and techniques. Whethe
9 min read
Bash shell script to find sum of digits
Given a number Num, find the sum of digits of the number.Examples: Input : 444 Output : sum of digits of 444 is : 12 Input : 34 Output : sum of digits of 34 is : 7 Approach: 1. Divide the number into single digits 2. Find the sum of digits . C/C++ Code # !/bin/bash # Program to find sum # of digits
1 min read
Shell script to print table of a given number
In this article, we will explore the process of creating a simple yet powerful shell script that takes user input and generates number tables. Number tables are a common mathematical concept used to display the multiples of a given number up to a specified limit. We will illustrate this process with
2 min read