How to Run Shell Script in Linux
If youre new to Linux, one of the things youll need to learn how to do is run shell scripts. Shell scripts are a way of automating tasks in Linux, allowing you to save time and effort by writing scripts that do things like move or copy files, rename files, create backups, or install applications. In this article, well go over the steps to write and execute a shell script in Linux, as well as some tips and best practices for running shell scripts.
Before we get started, let’s define what a shell script is. A shell script is a text file that contains a sequence of commands for a shell to execute. The shell is a program that interprets the commands you enter on the command line interface, or CLI, of the Linux operating system. The commands in a shell script can be the same as those you enter on the command line interface, but in a shell script, they are executed automatically, one after the other, without the need for user input.
Now that we have a basic understanding of what a shell script is, let’s dive into how to write and execute one in Linux.
Steps to Write and Execute a Shell Script
To write and execute a shell script, youll need to follow a specific set of steps. In this section, well go over the steps outlined in javatpoint.com to create a .sh file, write the script, make it executable, and run it.
Creating a .sh File
The first step in writing a shell script is to create a .sh file. This file will contain the script you want to execute. To create a .sh file, you can use any text editor, such as vi, nano, or gedit. Heres how to create a .sh file using the vi text editor, as explained in itsfoss.com:
- Open a terminal window.
- Type
vi filename.sh
(replace filename with the name you want to give your file) and press Enter. - Press
i
to enter insert mode. - Type your script.
- Press
Esc
to exit insert mode. - Type
:wq
and press Enter to save and exit the file.
Writing the Script
Once you have your .sh file, you can start writing your script. Your script can contain any command that you can enter on the command line interface, such as moving files, renaming files, or creating directories. Heres an example script that prints Hello, World!, as given in javatpoint.com:
#!/bin/bash
echo "Hello, World!"
Making it Executable
Before you can run your script, youll need to make it executable. To do this, youll use the chmod
command. DigitalOcean provides a guide on how to make your script executable:
- Open a terminal window.
- Type
chmod +x filename.sh
(replace filename with the name of your file) and press Enter.
Running the Script
Now that your script is executable, you can run it by typing ./filename.sh
(replace filename with the name of your file) and pressing Enter. Your script will execute, and youll see the output in the terminal window.
Congratulations! Youve just written and executed your first shell script in Linux. In the next section, well go over two ways to run a shell script in Linux.
Two Ways to Run a Shell Script in Linux
In this section, well discuss two ways to run a shell script in Linux. As explained in itsfoss.com, it’s important to note that before running a script, you need to make sure that you have the permissions to execute it.
Using the Terminal
The first and most common way to run a shell script in Linux is to use the terminal. Once you have your script ready, follow these steps:
- Open a terminal window.
- Navigate to the directory where your script is located using the
cd
command. - Type
./filename.sh
(replace filename with the name of your file) and press Enter. - Your script will execute, and youll see the output in the terminal window.
It’s important to note that before you execute the script, you need to make sure that you have the permissions to execute it. You can use the chmod
command as explained in DigitalOcean to make the script executable.
Using the GUI
The second way to run a shell script in Linux is to use the graphical user interface (GUI). The process varies depending on the Linux distribution youre using, but in general, you can follow these steps:
- Open the file manager and navigate to the directory where your script is located.
- Right-click on the script file and select Properties from the context menu.
- In the Permissions tab, check the Allow executing file as program option.
- Close the Properties window.
- Double-click on the script file to run it.
Congratulations! You now know two ways to run a shell script in Linux. In the next section, well discuss three methods for running .sh scripts on Linux.
Three Methods for Running .sh Scripts on Linux
In this section, well discuss three methods for running .sh scripts on Linux, as outlined in askubuntu.com.
Method 1: Run the Script in the Terminal
The first method to run a .sh script on Linux is to use the terminal. Heres how to do it:
- Open a terminal window.
- Navigate to the directory where your script is located using the
cd
command. - Type
./filename.sh
(replace filename with the name of your file) and press Enter. - Your script will execute, and youll see the output in the terminal window.
Method 2: Run the Script with Bash
The second method to run a .sh script on Linux is to run it with bash. Heres how to do it:
- Open a terminal window.
- Navigate to the directory where your script is located using the
cd
command. - Type
bash filename.sh
(replace filename with the name of your file) and press Enter. - Your script will execute, and youll see the output in the terminal window.
Method 3: Run the Script with Sh
The third method to run a .sh script on Linux is to run it with sh. Heres how to do it:
- Open a terminal window.
- Navigate to the directory where your script is located using the
cd
command. - Type
sh filename.sh
(replace filename with the name of your file) and press Enter. - Your script will execute, and youll see the output in the terminal window.
Its important to note that running scripts from untrusted sources can be dangerous. Always make sure that you trust the source of the script before running it. Additionally, the terminal is a powerful tool, and its recommended to use it for advanced tasks.
Tips for Writing and Running Shell Scripts in Linux
In this section, well provide some tips for writing and running shell scripts in Linux, as suggested in itsfoss.com.
Use Comments
Adding comments to your script is a great way to make it more readable and easier to understand. Comments are lines in your script that are ignored by the shell, and they start with the #
character. Heres an example of a comment:
#!/bin/bash
# This is a comment
echo "Hello, World!"
Use Variables
Variables are a great way to make your script more flexible and easier to maintain. A variable is a name that represents a value, and you can use it anywhere in your script. Heres an example of a variable:
#!/bin/bash
NAME="John"
echo "Hello, $NAME!"
Use Functions
Functions are a great way to reuse code in your script. A function is a set of commands that you can call multiple times in your script. Heres an example of a function:
#!/bin/bash
greet() {
echo "Hello, World!"
}
greet
Use Loops
Loops are a great way to repeat a set of commands multiple times in your script. There are two types of loops in Bash: the for
loop and the while
loop. Heres an example of a for
loop:
#!/bin/bash
for i in 1 2 3 4 5
do
echo "Number: $i"
done
Use Conditional Statements
Conditional statements are a great way to make your script more intelligent. There are two types of conditional statements in Bash: the if
statement and the case
statement. Heres an example of an if
statement:
#!/bin/bash
NAME="John"
if [ "$NAME" == "John" ]
then
echo "Hello, John!"
else
echo "Hello, Stranger!"
fi
Congratulations! You now know some tips for writing and running shell scripts in Linux. In the next section, well discuss the importance of maintaining written records and provide a list of file formats for different types of files.
The Importance of Maintaining Written Records
In this section, well discuss the importance of maintaining written records and provide a list of file formats for different types of files, as explained in docs.fileformat.com.
Why Maintain Written Records?
Maintaining written records is important for several reasons:
- Documentation: Written records provide a way to document important information about a project, process, or system. This information can be shared with others and used to ensure that everyone is on the same page.
- Accountability: Written records provide a way to hold people accountable for their actions. If something goes wrong, written records can be used to determine who was responsible and what actions were taken.
- Compliance: In some industries, maintaining written records is a legal requirement. Failure to maintain proper records can result in fines, legal action, or even criminal charges.
File Formats for Different Types of Files
Heres a list of file formats for different types of files:
- 3D: STL, OBJ, PLY
- Audio: MP3, WAV, AIFF
- CAD: DWG, DXF, DWF
- Compression: ZIP, RAR, 7Z
- Database: CSV, SQL, XML
- Disc and Media: ISO, IMG, DMG
- eBook: EPUB, MOBI, PDF
- Email: EML, MSG, PST
- Executable: EXE, DMG, MSI
- Finance: QIF, OFX, CSV
- Font: TTF, OTF, WOFF
- Game: APK, IPA, XAP
- GIS: SHP, KML, GPX
Its important to note that this list is not exhaustive, and there may be other file formats that are relevant to your particular industry or field. Additionally, its important to choose the right file format for your needs, as different file formats have different features and capabilities.
Conclusion
In this article, weve discussed how to run shell scripts in Linux and provided tips for writing and running shell scripts. Weve also discussed the importance of maintaining written records and provided a list of file formats for different types of files.
By following the methods outlined in this article, you should be able to run shell scripts on Linux with ease. Additionally, by using comments, variables, functions, loops, and conditional statements in your scripts, you can make them more flexible and easier to maintain.
Remember, its important to always be cautious when running scripts from untrusted sources and to use the terminal for advanced tasks.
We hope this article has been helpful in your journey to becoming a more proficient Linux user. If you have any questions or feedback, please feel free to leave a comment below.
More Linux Tutorials
We hope this article has been informative and helpful in your journey to mastering shell scripts in Linux. If youre interested in learning more about Linux, check out some of our other tutorials:
- How to Install and Use Docker on Linux
- A Beginner’s Guide to Linux File Permissions
- How to Set Up a Firewall on Linux Systems
Thank you for reading, and dont forget to check out our other great content!
Common Questions
Who can run shell scripts in Linux?
Anyone who has access to a Linux terminal can run shell scripts.
What is a shell script in Linux?
A shell script is a program written in the shell language that can be executed in a Linux terminal.
How do I make my shell script executable?
Use the chmod +x
command to make your shell script executable.
What is the #! /bin/bash syntax in a shell script?
This syntax tells the system which interpreter to use to execute your shell script.
How can I check if my shell script has any syntax errors?
Use the bash -n
command to check for syntax errors in your shell script.
What if I want to run a shell script from a different directory?
Use the ./
prefix followed by the path to your shell script to run it from a different directory.