How to write a basic shell script
Learn how to write a basic Bash shell script. Although shell scripts can be long, complicated scripts which perform complex tasks, you can also learn how to write a simple shell script in 10 minutes. If you can run commands at the command line, learning how to write a basic Bash script is easy.
Requirements
- A Cloud Server running Linux
- A basic familiarity with the command line
- Automatic backup & easy recovery
- Intuitive scheduling and management
- AI-based threat protection
Create the file
Go to your home directory:
Using cd without any arguments will automatically take you to your home directory.
Create a directory for test scripts:
Move into this directory:
Create a file named myscript.sh and open it for editing:
The first line of any shell script must be #! followed by the path to the shell that you want to use for the script.
Since we will be writing this script for the Bash shell, the first line of the file will be:
Any other line which begins with a hash mark # will be ignored by the script. This allows you to add comments and notes to your script. After the first line, it is a good practice to add a few comments about the script's purpose:
Add the command(s) and make it executable
Any command which you run from the command line, you can put into a shell script. This is convenient for:
- Commands you use frequently.
- Lengthy commands, especially those which use lots of flags.
- Commands which you want the server to run automatically as a cron job.
- Commands you can never quite remember, and have to look up each time.
For this example, we will use a command that will echo the words "Hello world" to the command line. Put the following into the file:
Save and exit the file.
The script needs to be executable in order to run. Give the script executable permissions with the command:
Run the script
To run the script, simply invoke it by its file path. Since you are in the same directory as the script, you can run it from here by typing:
You can run the script from anywhere on the server by typing the full file path:
Add the script to your PATH
Unlike your shell script, most Linux commands do not require you to type out the full file path each time you want to run them. This is because those scripts are in your PATH.
The PATH is the list of directories that the shell checks for a command each time you type it. You can see your PATH by using the command:
This will output the list of directories which are in your PATH:
There are two ways to add your shell script to your PATH:
1. Move the shell script to a directory that is already in your PATH
You can move the shell script into any of the directories which are listed in response to the echo $PATH command. The best practice is to put your personal shell scripts into the /usr/local/bin directory:
You can then run the script from anywhere with the command:
2. Add the shell script's directory to your PATH
You can add any directory to your PATH with the command:
For example, if user jdoe wants to add /home/jdoe/myscripts to the PATH, the command is:
You can then run the script from anywhere with the command:
- Unlimited traffic
- Fast SSD NVMe storage
- Free Plesk Web Host Edition
Use input and variables
Your scripts can use variables, and you can prompt the user for input. This creates an interactive script which allows you to give it new information each time it is run.
Let's start by creating a new file with the name greetme and opening it for editing:
As before, make sure that the first line of the script is the path to the shell:
Add a comment:
The first line of the script will be what the script says when you invoke it:
We use echo -e for this, because -e allows the command to handle special characters.
The next line tells the script to accept your input at a prompt:
The final line is the script's response:
The entire script reads as follows:
Save and exit the file. Then make it executable with the command:
You can now run the file and see the results: