This is the start of a new series of tutorials on Free and Open Source Software. In this article, you will learn about bash scripting.
The series assumes that you are somewhat familiar with the Linux terminal. You don’t have to be a master, but knowing the basics will do just fine. I recommend going through the Terminal Basics series.
Who is this series for?
Anyone who wants to start learning bash shell scripting.
If you are a student and have shell scripting as part of your course curriculum, this series is for you.
If you’re a regular desktop Linux user, this series will help you make sense of most shell scripts you encounter while troubleshooting various programs and fixes. You can also use it to automate some common, repetitive tasks.
By the end of the Bash Basics series, you should be able to write simple to intermediate bash scripts.
All chapters of the series contain simple exercises that you can learn by doing.
You will learn bash shell scripting here. While there are other wrappers with mostly the same composition, their behavior still differs in a few points. Bash is the most popular and universal shell, hence start learning scripting with bash.
Your first script: Hello World!
Open Terminal. now Create a new directory To save all scripts you will create in this string:
We Invited Create a new file here:
now, Edit the file and add
echo Hello World her line. You can do this using the cat command’s append mode (using >):
[email protected]:~/bash_scripts$ cat > hello_world.sh echo Hello World ^C
I prefer adding newlines while using the cat command to add text.
Press Ctrl + C or Ctrl + D to exit the append mode of the cat command. Now if you check the contents of the script
hellow_world.shyou should only see one line.
The moment of truth has arrived. I’ve created my first shell script. It’s time to Run the shell script.
do like this:
The echo command simply displays whatever is supplied to it. In this case, the Hello World shell script should output to the screen.
Congratulations! You have successfully run your first script. How cool!
Here’s a rerun of all the above commands for your reference.
Another way to run your scripts
Most of the time you will run shell scripts this way:
Which will result in an error because your file as a script does not have execute permission yet.
bash: ./hello_world.sh: Permission denied
Add the execute permission for yourself to the script:
chmod u+x hello-world.sh
Now, you can run it like this:
So, you’ve learned two ways to run a script. It’s time to focus on bash.
Convert your shell script to a bash script
Confused? In fact, there are many shells available in Linux. Bash, ksh, csh, zsh, and many more. Of all, bash is the most popular and is installed by default in almost all distributions.
The shell is a translator. It accepts and manages Linux commands. While the syntax of most shells remains the same, their behavior may differ at certain points. For example, the processing of parentheses in conditional logic.
This is why it is important to tell the system which shell to use to interpret the script.
when you are using
bash hello_world.shI used the bash compiler explicitly.
But when you run shell scripts this way:
The system will use whatever shell you’re currently using to run the script.
To avoid unwanted surprises due to handling different syntax, you must explicitly tell the system which shell script.
How is it done? Use shebang (#!). Normally, # is used for comments in shell scripts. However, if #! It is used as the first line of the program, and has a special purpose of telling the system which shell to use.
So, change the content of hello_world.sh so that it looks like this:
#!/bin/bash echo Hello World
Now, you can run the shell script as normal knowing that the system will use the bash shell to run the script.
If you feel uncomfortable editing script files in the terminal, as a desktop Linux user, you can use Gedit or other GUI text editors to write and run scripts in the terminal.
🏋️ Workout time
It’s time to practice what you’ve learned. Here are some basic exercises for this level:
- Write a bash script that prints “Hello Everyone”
- Write a bash script that displays the current working directory (hint: use the pwd command)
- Write a shell script that prints your username in the following way: my name is XYZ (hint: use $USER)
Answers can be discussed in This custom topic in the community forum.
recent practice uses
$USER. This is a private variable that prints the username.
Which brings me to the topic of the next chapter in the Bash Basics series: Variables.
Look out for that next week.