When you work on Linux systems, it’s important to know some Bash scripting. Bash is the command line interpreter on most Linux systems. Bash stands for Bourne Again Shell. Linux systems may have more than one shell available. Besides Bash, another popular one is zsh. zsh is popular because it takes a lot from the other shells and makes a really powerful shell with advanced theming, programming, and more.
What is the shell?
In the most basic form, the shell is just the command line interface (CLI) presented to you when you use a Linux system. If you’re using a Linux system right now, just find the terminal application. That is the shell. If you SSH to a remote system, that is the shell for the remote system.
This is the shell on my Fedora laptop. If it looks a little different than what you’ve probably seen, that’s because I use the Xfce desktop environment. Each desktop environment has their own default terminal program.
In the shell, try running some programs like
pwd. Just press enter once you type in the command. What happens?
Here’s what happened for me. You can see that
date gave us the current date in long-term format.
cal gave us a calendar.
pwd showed us where we’re at in the filesystem.
What is scripting?
Imagine that you had to run the same task every day that involved all three of those commands. It would get a little repetitive and boring, right? Scripting helps us by performing repetitive tasks over and over again and letting the system run those tasks instead.
Instead of typing all three of those commands, I now only have to run a single command – calling my script! Now I can get my daily task done a lot faster.
What does this script actually look like? I’ll show you.
!#/bin/bash date cal pwd
Yes, it’s that simple. Let me explain what you’re looking at. The first line is called the shebang which is just the
#! and it tells the script what interpreter to use. So if you’re in a zsh shell, this script will run with bash. For this guide, we’re always going to use bash so the first line you’re going to see in our bash files is
The next three lines should be self-explanatory. These are the same commands we manually ran before. Except instead of having to type them ourselves, the computer did it for us.
Now how do we actually run the script? First, we need to make the script executable. If we don’t set this permission, the script will open up in our text editor (if we’re using a GUI) or will just not run if we’re calling it from the CLI. To set the permission, we use
chmod +x FILE and replace FILE with the name of the file we want to set the permission on.
Now to actually call the script, we run it as
./FILE.sh from the CLI.
How to write our first script
To write your first script, you just need a Linux system and a text editor. If you don’t have a spare computer you can use or a virtual machine, you can always spin up a VPS from our site sponsor, NodeSpace. Being on a Mac, you don’t even need anything as macOS is UNIX-based and has bash installed (although a few years ago, Apple changed the defualt shell to zsh, but bash scripting works very well). If you’re using Windows 10 or 11, you can use Windows Subsystem for Linux to run a VM.
Once you’re on a Linux system, the first thing I like to do is create a “scripts” directory in my home directory. Your home directory is indicated by the ~ in the CLI. So if you’re not sure where your home directory is, just type in
cd ~ and press enter and then you’ll go to your home. So once you’re in there, type in
mkdir scripts and press enter. Now you have a place to store your scripts. You can expand on this by using version control such as git if you’re comfortable and familiar enough.
Change to your scripts directory and run
vi helloworld.sh and press enter. Now the reason I will always use vi is because most distros include it by default. Other distros don’t always include nano and if you’re on a system where you can’t download nano, you should know how to use vi.
Once in vi, press i to enter insert mode. Now you can start typing your script. Remember, the first line we need is the shebang. So type in:
Now we are telling our script to use the bash interpreter. Now we can write more of our script. Type in the following:
echo "Hello, world! This is my first bash script!" echo "This script is being run from: " pwd
Now press the escape key, and type in
:wq! and press enter. Remember what we said about making our scritp executable? Reference back up and see if you can make this script executable. Once you make it executable, try to run your script. What happens?
Your first script results
Does your script output look anything like this?
Hello, world! This is my first bash script! This script is being run from: /home/travisnewton/scripts
If not, go back and see where you may have made a mistake.
Great job on completing the first lesson in Bash Scripting! Follow along for more!