Creating a Shell Script
Login to your Linux machine and open the terminal, navigate to the folder where you want to store the shell script. Shell scripts end with the extension “.sh”. Let’s create our first shell script. Type in
touch script.sh
Now, this script file is not executable by default, we have to give the executable permission to this file. Type in
chmod +x script.sh
Now, we will add some commands to this shell script. Open this shell script with any text editor of your choice (command-line based or GUI based) and add some commands. We will use nano. Type in
nano script.sh
Add the following commands to test this shell script
echo This is my first shell script touch testfile ls echo End of my shell script
Save the changes, and run the shell script by typing in
./script.sh
You can see, it executed all the specified commands.
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. You can list those commands and execute them all with just a single script. Let’s see how you can create a shell script and run it on Linux.