Over the next 5 days, you’re going to learn the basics of shell scripting.
To get the most out of this course, follow along. Type in the scripts. Run them. Experiment and have fun!
Why? Well, when you actively participate you learn faster and retain more of what you learn.
Just so you know who you’re learning from, I want to briefly introduce myself. My name is Jason Cannon. I’m the author of several Linux guides and the creator of over a dozen Linux courses. Enough about me… you’re here to learn about shell scripting, so let’s get started!
What Is a Shell Script, Exactly?
A shell script is simply a plain text file that contains a series of commands and shell statements.
When a shell script is executed, it, in turn, executes the commands listed in the script. It starts at the top and executes the commands on each line, one line a time, until the end of the file. Executing a shell script produces the exact same result as you typing each line directly into the terminal.
This means any work you do on the command line can be automated with a shell script!
Write Your First Script
In just a minute, you’re going to create a shell script named “day1.sh”. To do that, open up a terminal on your Linux system. Next, use your favorite text editor to create the file.
If you don’t have a favorite text editor, I suggest starting with nano as it’s simple to use. Plus, it displays help at the bottom of the screen, so you always know what your options are at any given time. The only downside is that you might have to install it first. To do that on a RedHat based Linux distro such as CentOS execute:
$ sudo yum install -y nano
To install nano on a Debian based Linux distro such as Ubuntu, Linux Mint, or Kali execute:
$ sudo apt-get install -y nano
(NOTE: The “$” represents the command prompt. Type what follows “$”. Any line that does not start with “$” is output and does not need to be typed in.)
$ nano day1.sh
Type the following contents into the file. Be very careful to use the same spacing, case, and punctuation as shown below:
#!/bin/bash echo "I don't have to be great to start, but I have to start to be great!"
Save your changes to the file. If you’re using nano, hold down the control key and type the letter “x”. You’ll be asked if you would like to save your changes. Type the letter “y” to answer yes. Finally, press ENTER to save the file as “day1.sh”.
Before you can execute this file as a script, you need to give it execute permissions. Do that by typing:
$ chmod +x day1.sh
Now you’re ready to run your script:
Congratulations on writing and executing your first shell script!
If everything goes as planned, you’ll see the following text on your screen:
I don't have to be great to start, but I have to start to be great!
If you forget to add the execute permission to the file you’ll see this:
$ ./day1.sh -bash: ./day1.sh: Permission denied
That just means you need to run chmod +x day1.sh and try running your script again.
Naming Your Shell Scripts
We can use this little 2-line shell script as an excuse to learn some important things about scripting. For starters, let’s talk about the name of the script: “day1.sh”.
There’s nothing special about the file name for this (or any) shell script. By convention shell scripts can end in “.sh,” but the file extension is not significant like it is in some other operating systems. As a matter of fact, you can name your shell script anything so long as it’s a valid file name. All these are valid shell script file names:
day-1.sh day_1 DayOne _d1
Shell Script Permissions
When it comes to shell scripts, file names don’t matter. What does matter are the file’s permissions! More specifically, a shell script must have executable permissions.
You can use “ls -l” to view the current permissions on a file. For example, here are the permissions on the day1.sh file before they were changed:
$ ls -l day1.sh -rw-r--r-- 1 jason teacher day1.sh
The permissions string is “-rw-r–r–“. More on that in a second…
Here are the permissions on the day1.sh file after they were changed:
$ chmod +x day1.sh $ ls -l day1.sh -rwxr-xr-x 1 jason teacher day1.sh
The permissions string for this file is now “-rwxr-xr-x”.
With permissions, “r” stands for read, “w” stands for write, and “x” stands for execute.
The first character in the permission string tells you if it’s a file (-) or a directory (d).
The next three characters represent the permissions of the owner of the file.
The following three characters represent the permissions that are granted to the group of the file.
The last three characters represent the permissions that everyone else on the system has to this file.
This graphic sums it up nicely:
In this example, the jason user has read, write, and execute permissions, the teacher group has read and execute permissions, and everyone else (who is not the jason user or in the teacher group) has read and execute permissions to this file.
In order to execute the file (script), you need to have both read and execute permissions to that file. If you can’t read the file, you can’t execute it because you can’t see the contents that are going to be read in by the interpreter.
If you still get a “Permission denied” after running “chmod +x day1.sh,” you’ll need to add the read permissions as well by running “chmod +rx day1.sh”.
What You’ve Learned Today
In this first lesson, you learned the absolute basics of creating a shell script. You learned how to name your shell scripts, how to give them execute permissions, and how to run them.
You’re off to a great start, but we haven’t even talked about the lines inside the shell script yet! (That funny-looking first line is really important!)
Here’s What To Expect Next…
In tomorrow’s lesson, you’ll learn why shell scripts start with “#!/bin/bash”. You’ll also find out what “./” means, when to use it, and why. Of course, you’ll be introduced to even more new shell scripting techniques too.
Be on the look out for tomorrow’s email. 😉
Until then, have a great day!
P.S. Because shell scripts are primarily a series of Linux commands, here’s a Linux command cheat sheet to help you along the way.
P.P.S. If you’re ready to master shell scripting once and for all check this out.