Have you ever wanted to learn how to script in Linux? It's easier than you think. Sometimes, scripts (often referred to as shell or bash scripts) are actual programs with complex code inside. Other times, they're just a long list of tasks that users have put together to make getting things done on Linux faster and easier. Here's the best beginner's guide to shell scripting in Linux.
Why are you making a script?
Scripting in Linux is a very useful skill. You can automate some repetitive tasks using bash scripts. If done well, it can make you highly productive and allow you to accomplish more in less time.
For example, you may have just created an Arch Linux system from scratch. The operating system, along with all essential packages, is installed and can boot into the machine when the operating system starts. Setting up Arch Linux takes time, so the process is not complete.
At this point, the user can write a Bash script and get everything done simultaneously. None of this is programming—or advanced, for that matter. However, given the user's sufficient understanding of how Arch Linux works, they will be able to automate almost the entire post-setup process (desktop environment, drivers, user setup, etc.).
The only limit to your bash program is your knowledge of Linux and Bash! Creating one is easier than you think.
start
Objects must be defined and resources loaded when writing code. Some objects must also be defined when programming with the shell. In Bash programming, this is known as a "shebang." The shebangs used in scripts tell the script which interpreter to execute under. This could be Bash or any other script available on your system. Note that different languages have their own language interfaces.
For example: When writing a text withPython Then the word 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 to remove it from Linux. All modern Linux distributions are based on the same version of bash, and the bash shell is usually located in the same place.
Another shebang that proves to be 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 complex process—if the writer does it. Most often, scripts are simply a collection of different operations: transferring a file, downloading something, installing software, and so on.
- Keep in mind that Bash is a language designed for handling files and system operations. If Bash meets your needs, that's fine. However, understand that for advanced programming, Bash isn't really the right choice, and you'd be better off switching to something like Python.
- Make your scripts "SH" compatible and in ".sh" format if you plan to use scripts on more than just Linux. While other UNIX-like operating systems may have "bash-like" shells, some don't have bash at all, and it's a good idea to be prepared for that.
- Learn about the Bash shell and how it works. It will help you write better scripts.
- Always use shebang, and most importantly, use the right one. It can mean the difference between a good script and a bad one that doesn't work properly.
- Always comment on each process. In six months, you may come back to your transcript and wonder what everything means, so it's crucial that the transcript is well documented and easy to understand (for you and anyone else who might see it).
Make your code readable. Even if your code isn't complex, it should still make sense, and making it easier than you think. - Test your script for errors before giving it to others. Don't have others test for errors for you. Ideally, your scripts should work before you send them out for people to use.
Create a script
To get started with scripting, all you need is a text editor. Any simple text editor will do. It doesn't have to be complex or comprehensive. In this example, we're creating a simple welcome text using Gedit.
Create and open the script in Gedit
Create a text named greetings.sh Open it inside the Gedit editor. This first part of the script is the shebang, as we 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 using the script to understand what the code is intended to do. Comments can be added to a script by adding the # symbol. Nothing will be captured by the script after this.
# My first Linux script
Print a simple welcome word
To print any output, we use the echo command in bash. If you want to print "Hello world," the command should look like this:
echo "Hello world"
Make the script executable
To make the script executable, we use the chmod command.
chmod +x greetings.sh
+x means to add execute 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 a core feature of a bash script, and you should understand the concept of variables in bash. Variables are like storage where you can store values. In this case, we store user input in a bash variable. To provide input, we ask the user a question.
echo -e "What is your name: "
When the user enters their 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 like, but be sure to add the read command right after the echo command.
Print welcome message
The stored username is the input name variable. To greet the user, we use the echo command again.
echo "Good morning $inputname"
We use the $ sign to represent a variable within a string. Save the script and run it with the command ./greetings.sh.
Now you can make your own greetings and share them with your friends.
File attachments
There is no difference in file extensions for scripts. Naming a file with the ".sh" file extension doesn't significantly affect how the program runs. A Bash script with no file extension—empty text files and everything in between—will continue to run as long as the correct commands and arguments are present.
Although the Bash shell ignores file extensions, this doesn't mean a script writer should. Some desktop environments that allow you to set shell scripts to run at startup rely on the script to have the correct ".sh" file extension. This also helps for organization purposes.
When it comes to this, most shell scripts are saved as ".sh" files for portability. "sh" has nothing to do with Bash itself, and the script can be run with any compatible shell.
Alternatively, scripts can be saved as .bash, .ksh (Unix korn shell), etc. These file extensions are inferior and actually limit the usefulness of the script. This is because they are designed only for shells that use those extensions.
Frequently Asked Questions
Q1. Is Bash scripting easy to learn?
answer. If you're familiar with programming languages, Bash programming will seem fairly easy. It shares some of the same programming concepts, such as variables, loops, and so on. However, if you don't know anything about programming, I recommend starting with Python. Python is very easy to learn, and you can even create scripts using it.
Q2. Is Bash worth learning?
answer. If you use Linux daily or use Linux in your work environment, you should learn Bash scripting. You can also learn Bash to automate your daily tasks. Beyond that, I don't see any use for Bash in the software development industry.
Q3. How can I learn Bash scripting in Windows?
answer. There is no native way to learn bash in Windows, but you can use the Windows Subsystem for Linux (WSL) to run Linux distribution inside your Windows machine and use it to run bash commands.