A Beginner’s Guide to Shell Scripting in Linux

Be a shell expert in no time!

Script (1)

Have you ever wanted to learn “scripting” in Linux? Making them is easier than you might think. Sometimes scripts (often referred to as shell or bash scripts) are real programs with complicated code inside. Other times they’re just a long list of tasks that users put together to make getting things done under Linux faster and easier.

In this article you will learn how to make a basic shell script under Linux. First, you’ll learn about the basic requirements and do’s and don’ts of a shell script, then how to take user input via the script and print it out on the terminal screen. You will learn how easy it is to get started (and the best practices) scripting in Linux.

Tip: check out our regular expressions cheatsheet.

Why Would You Make a Script?

Making scripts in Linux is a very useful skill to have. You can automate some repetitive tasks using bash scripts. If you do it well, it can make you very productive and let you achieve more in less time.

For example: maybe you just built Arch Linux from scratch. The operating system is installed, along with all of the basic packages, and it can boot to the terminal when the operating system starts up. Arch Linux takes time to set up, so the process isn’t completed.

At this point, the user could write a Bash script and accomplish everything at once. None of this is programming – or advanced for that matter. However, given the fact that the user understands enough about the way Arch Linux works, they’d be able to automate almost the entire post-setup process (desktop environment, drivers, user setup, etc.).

The only limit to your bash script is your own Linux and Bash knowledge! Making them is easier than you may think.

Getting Started

Things need to be specified and resources loaded when writing code. Some things need to be specified as well when scripting with the shell. In bash scripting, this is known as a “shebang.” The shebangs used in scripts tells the script what interpreter it should execute under. This could be Bash or any other scripts available in your system. Do note that different languages have their own shebangs.

For example: when writing a Python script, the shebang would be #!/usr/bin/python.

Bash has many different shebangs that can be used, but most users have probably only seen #!/bin/bash. As a general rule, use #!/bin/bash when writing a simple script and don’t plan on taking it off Linux. All modern Linux distributions are on the same version of bash, and the bash shell is usually located in the same place.

Another shebang that proves itself useful is #!/usr/bin/env bash. It is designed for portability and should be used if the script is designed to run on other Unix-like operating systems (BSDs,  macOS, etc.).

Best Practices

Writing scripts in Bash can be a complicated process – if the writer makes it that way. More often than not, scripts are just a collection of different operations. Moving a file, downloading something, installing programs, and that sort of thing.

  • Keep in mind that Bash is a language that is designed to manipulate files and processes on the system. If Bash meets your needs, that is good. However, do understand that for advanced programming, Bash really isn’t the right choice, and it’d be best to move on to something like Python.
  • Make your scripts “SH” compatible and in the “.sh” format if the plan is to use scripts on more than just a Linux platform. Though other UNIX-like operating systems may have “bash-like” shells, some don’t have bash at all, and it’s good to be prepared for this.
  • Learn the Bash shell and how it works. It’ll help you write your scripts better.
  • Always use a shebang, and more importantly, use the right one. It can mean the difference between a good script and a terrible one that doesn’t work right.
  • Always comment out every operation. In six months you may come back to your script and wonder what everything means, so it is crucial that your script is documented well and easy to understand (for you and anyone else who may see it).
  • Make your code readable. Even if your script isn’t anything complex, it should still make sense, and making them is easier than you may think.
  • Test your script for errors before giving it to others. Don’t make others bug test for you. Ideally, scripts should work before you send them out for people to use.

Making a Script

To start scripting, all you need is a text editor. Any simple text editor will do. It doesn’t have to be a complicated or comprehensive one. For this example, we are making a simple greetings script using Gedit editor.

Create and open the script in Gedit

Create a script named “greetings.sh” and open it inside Gedit editor. This first part of the script is the shebang, as mentioned earlier. This allows the script to tell the interpreter what it should use to understand the code.

#!/bin/bash

Next, let’s write a comment. This will allow anyone who uses the script to understand what the code is meant to do. Comments can be added to a script by placing a # symbol. Anything after it won’t be picked up by the script.

# My first Linux script

Printing a Simple Hello World

To print any output, we use the echo command in bash. If you want to print out “Hello world,” the command should look like this:

echo "Hello world"

Make the Script Executable

To make the script executable, we are using the chmod command.

chmod +x greetings.sh

The +x means to add execution permission to the script file. To run the script, call the script name with its relative path.

./greetings.sh

Taking User Input

Taking user input is the most basic feature of a bash script, and you should have a concept of variables in bash. Variables are like storage where you can store values. In this case, we are storing the user input in a bash variable. To make an input, we ask the user a question.

echo -e "What is your name: "

When the user enters his name, we can read the input using a bash variable called inputname.

echo -e "What is your name: "
read inputname

You can use any variable name you prefer, but make sure to add the read command just after the echo command.

Printing a Greetings Message

The stored user name is the inputname variable. To greet the user, we again use the echo command.

echo "Good morning $inputname"

We are using the $ sign to represent a variable inside a string. Save the script and run this using the ./greetings.sh command.

Script

Now you can make your own greetings and share them with your friends.

File Extensions

There isn’t a difference in file extensions for scripts. Naming a file with the “.sh” file extension does little to affect the way the program runs. A Bash script will still run with no file extension – blank text files and everything in between as long as the right commands and arguments are present.

Even though the Bash shell ignores file extensions, that doesn’t mean the script writer should. Some desktop environments that allow you to set shell scripts to run at startup depend on the script to have the correct “.sh” file extension. This also helps for organization purposes.

When it comes down to it, most shell scripts are saved as “.sh” files for portability. The “sh” doesn’t have anything to do with Bash itself, and the script can run with any compatible shell.

Alternatively, scripts can be saved as .bash, .ksh (Unix korn shell), etc. These file extensions are inferior and really limit the usefulness of a script. This is due to them being designed only for the shells that use those extensions.

Frequently Asked Questions

Is it easy to learn bash scripting?

If you had an exposure to programming languages, then bash scripting will seem fairly easy. It shares the same concepts of programming, like variables, loops, etc. But if you don’t know anything about programming, I recommend you start with python. Python is very easy to learn, and you can also make scripts with it.

Is bash worth learning?

If you are driving Linux daily or use Linux in your work environment, then you should learn bash scripting. You can also learn bash to automate your daily task. Otherwise, I don’t see any use of bash in the software development industry.

How can I learn bash scripting in Windows?

There is no native way to learn bash in Windows, but you can use Windows subsystem for Linux (WSL) to run a Linux distribution inside your Windows machine and use it to run bash commands.

Image credit: Unsplash. All screenshots by Hrishikesh Pathak.

Hrishikesh Pathak
Hrishikesh Pathak

Developer and writer. Write about linux and web.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox