Bash scripts: start
Bash scripts, part 2: loops
Bash scripts, part 3: command line options and keys
Bash scripts, part 4: input and output
Bash Scripts, Part 5: Signals, Background Tasks, Script Management
Bash scripts, part 6: functions and library development
Bash scripts, part 7: sed and word processing
Bash scripts, part 8: awk data processing language
Bash scripts, part 9: regular expressions
Bash scripts, part 10: practical examples
Bash scripts, part 11: expect and automate interactive utilities
Today let's talk about bash scripts. These are
command line scripts written for the bash shell. There are other shells, for example - zsh, tcsh, ksh, but we will focus on bash. This material is for everyone, the only condition is the ability to work on
the Linux
command line .

Command line scripts are sets of the same commands that can be entered from the keyboard, assembled into files and combined by some common purpose. At the same time, the results of the work of the teams can be either of independent value or serve as input data for other teams. Scripting is a powerful way to automate frequently performed activities.

')
So, if we talk about the command line, it allows you to execute several commands at a time by typing them through a semicolon:
pwd ; whoami
In fact, if you tried it in your terminal, your first bash script, which involves two commands, is already written. It works like this. First, the
pwd
displays information about the current working directory, then the
whoami
displays information about the user under which you are logged in.
Using a similar approach, you can combine as many commands as you like in one line, the restriction is only in the maximum number of arguments that can be passed to the program. This limitation can be determined using the following command:
getconf ARG_MAX
The command line is a great tool, but commands have to be entered into it every time a need arises. What if you write a set of commands to a file and just call this file to execute them? In fact, the file we are talking about is called a command line script.
How are bash scripts
Create an empty file using the
touch
command. In its first line you need to specify which shell we are going to use. We are interested in
bash
, so the first line of the file will be:
#!/bin/bash
In the other lines of this file, the hash symbol is used to denote comments that the shell does not process. However, the first line is a special case, here the grid, followed by an exclamation mark (this sequence is called
shebang ) and the path to
bash
, indicate to the system that the script was created specifically for
bash
.
Shell commands are separated by a newline character, comments are marked with a grid sign. Here's what it looks like:
#!/bin/bash # This is a comment pwd whoami
Here, as well as on the command line, you can write commands in one line, separated by a semicolon. However, if you write commands on different lines, the file is easier to read. In any case, the shell will process them.
Setting Script File Permissions
Save the file, giving it the name of
myscript
, and the work on creating a bash-script is almost finished. Now it only remains to make this file executable, otherwise, trying to run it, you will encounter a
Permission denied
error.
Attempting to run a script file with improperly configured permissionsMake the file executable:
chmod +x ./myscript
Now we will try to execute it:
./myscript
After setting permissions, everything works as it should.
Successful launch of the bash scriptOutput messages
To output text to the Linux console, use the
echo
command. We will use the knowledge of this fact and edit our script, adding explanations to the data that display the commands already existing in it:
#!/bin/bash # our comment is here echo "The current directory is:" pwd echo "The user logged in is:" whoami
That's what happens after running the updated script.
Script outputNow we can display explanatory labels using the
echo
command. If you do not know how to edit a file using Linux tools, or have not met
echo
before, take a look at
this material.
Use of variables
Variables allow information to be stored in a script file, for example, the results of commands for use by other commands.
There is nothing wrong with the performance of individual teams without storing the results of their work, but the possibilities of such an approach are very limited.
There are two types of variables that can be used in bash scripts:
- Environment Variables
- Custom variables
Environment Variables
Sometimes in shell commands you need to work with some system data. Here, for example, how to display the current user's home directory:
#!/bin/bash # display user home echo "Home for the current user is: $HOME"
Please note that we can use the
$HOME
system variable in double quotes, this will not prevent the system from recognizing it. That's what happens if you run the above script.
Using the environment variable in a scriptAnd what if you need to display the dollar icon? Let's try this:
echo "I have $1 in my pocket"
The system will detect the dollar sign in the string delimited by quotes, and decide that we referred to the variable. The script will attempt to display the value of the undefined variable
$1
. This is not what we need. What to do?
In this situation, the use of a control character, a backslash, in front of a dollar sign will help:
echo "I have \$1 in my pocket"
Now the script will output exactly what is expected.
Using the escape sequence to display the dollar signCustom variables
In addition to environment variables, bash scripts allow you to set and use your own variables in a script. Such variables store the value until the script is completed.
As in the case of system variables, user variables can be accessed using the dollar sign:
TNW-CUS-FMP - promotional code for a 10% discount on our services, available for activation within 7 days #!/bin/bash # testing variables grade=5 person="Adam" echo "$person is a good boy, he is in grade $grade"
That's what happens after running such a script.
User variables in the scriptCommand substitution
One of the most useful features of bash scripts is the ability to extract information from the output of commands and assign it to variables, which allows you to use this information anywhere in the script file.
This can be done in two ways.
- Using the reverse apostrophe icon "` "
- Using the
$()
construction
Using the first approach, make sure not to insert a single quote instead of the reverse apostrophe. The command needs to be concluded in two such icons:
mydir=`pwd`
In the second approach, the same thing is written as:
mydir=$(pwd)
As a result, the script may look like this:
#!/bin/bash mydir=$(pwd) echo $mydir
During its operation, the output of the
pwd
will be stored in the variable
mydir
, the contents of which, with the help of the
echo
command, will go to the console.
Script that stores the results of the team in a variableMathematical operations
To perform mathematical operations in a script file, you can use a construct of the form
$((a+b))
:
#!/bin/bash var1=$(( 5 + 5 )) echo $var1 var2=$(( $var1 * 2 )) echo $var2
Mathematical operations in the scriptIf-then control structure
In some scenarios, you need to control the flow of command execution. For example, if a certain value is more than five, you need to perform one action, otherwise - another. This is applicable in very many situations, and
if-then
control construction will help us. In its simplest form, it looks like this:
if then fi
And here is a working example:
#!/bin/bash if pwd then echo "It works" fi
In this case, if the
pwd
completes successfully, the text “it works” will be displayed in the console.
We use the knowledge we have and write a more complex scenario. Let's say you need to find a certain user in
/etc/passwd
, and if you can find it, report that it exists.
#!/bin/bash user=likegeeks if grep $user /etc/passwd then echo "The user $user Exists" fi
That's what happens after running this script.
Search userHere we used the
grep
to search for a user in the
/etc/passwd
. If the
grep
is unfamiliar to you, its description can be found
here .
In this example, if the user is found, the script will display the corresponding message. And if you could not find the user? In this case, the script will simply complete the execution without telling us anything. I would like him to tell us about this, so we will improve the code.
If-else control construct
In order for the program to be able to report both the results of the successful search and the failure, we use the
if-then-else
. Here is how it works:
if then else fi
If the first command returns zero, which means its successful execution, the condition will be true and the execution will not follow the
else
branch. Otherwise, if something nonzero is returned, which means failure, or a false result, the commands located after
else
will be executed.
Let's write this script:
#!/bin/bash user=anotherUser if grep $user /etc/passwd then echo "The user $user Exists" else echo "The user $user doesn't exist" fi
Its execution went on the
else
branch.
Running a script with the if-then-else constructWell, we continue to move on and ask ourselves about more difficult conditions. What if you need to check not one condition, but several? For example, if the required user is found, you need to display one message, if some other condition is met - another message, and so on. In this situation, we will help nested conditions. It looks like this:
if 1 then elif 2 then fi
If the first command returns zero, which indicates its successful execution, the commands in the first block of
then
executed, otherwise, if the first condition turns out to be false, and if the second command returns zero, the second code block will be executed.
#!/bin/bash user=anotherUser if grep $user /etc/passwd then echo elif ls /home then echo fi
In a similar script, you can, for example, create a new user using the
useradd
command if the search did not return any results, or do something else useful.
Comparison of numbers
In scripts, you can compare numeric values. Below is a list of relevant commands.
n1 -eq n2
Returns the true value if n1
is n2
.
n1 -ge n2
Returns the true value if n1
greater than or equal to n2
.
n1 -gt n2
Returns the true value if n1
greater than n2
.
n1 -le n2
Returns the true value if n1
less than or equal to n2
.
n1 -lt n2
Returns the true value if n1 is less than n2
.
n1 -ne n2
Returns the true value if n1
not equal to n2
.
As an example, let's test one of the comparison operators. Note that the expression is enclosed in square brackets.
#!/bin/bash val1=6 if [ $val1 -gt 5 ] then echo "The test value $val1 is greater than 5" else echo "The test value $val1 is not greater than 5" fi
This is what this command will output.
Comparing Numbers in ScriptsThe value of the variable
val1
greater than 5, as a result, the
then
branch of the comparison operator is executed and a corresponding message is output to the console.
String comparison
In scripts, you can compare string values. Comparison operators look pretty simple, but string comparison operations have certain features that we will touch on below. Here is a list of operators.
str1 = str2
Checks strings for equality, returns true if strings are identical.
s tr1 != str2
Returns true if the strings are not identical.
str1 < str2
Returns true if str1
less than str2
.
str1 > str2
Returns true if str1
greater than str2
.
-n str1
Returns true if the length of str1
greater than zero.
-z str1
Returns true if the length of str1
is zero.
Here is an example of comparing strings in a script:
#!/bin/bash user ="likegeeks" if [$user = $USER] then echo "The user $user is the current logged in user" fi
As a result of the script, we get the following.
Comparison of lines in scriptsHere is one line comparison feature that is worth mentioning. Namely, the operators ">" and "<" must be escaped with a backslash, otherwise the script will not work properly, although error messages will not appear. The script interprets the “>” sign as an output redirection command.
Here is how working with these operators looks like in code:
#!/bin/bash val1=text val2="another text" if [ $val1 \> $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi
Here are the results of the script.
String comparison, displayed warningNotice that the script, although running, gives a warning:
./myscript: line 5: [: too many arguments
To get rid of this warning, put
$val2
in double quotes:
#!/bin/bash val1=text val2="another text" if [ $val1 \> "$val2" ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi
Now everything works as it should.
String comparisonAnother feature of the operators ">" and "<" is how they work with upper and lower case characters. In order to understand this feature, we will prepare a text file with the following content:
Likegeeks likegeeks
Save it by giving the name
myfile
, and then execute the following command in the terminal:
sort myfile
It will sort the lines from the file like this:
likegeeks Likegeeks
The
sort
command, by default, sorts the lines in ascending order, that is, the lowercase letter in our example is less than uppercase. Now we will prepare a script that will compare the same lines:
#!/bin/bash val1=Likegeeks val2=likegeeks if [ $val1 \> $val2 ] then echo "$val1 is greater than $val2" else echo "$val1 is less than $val2" fi
If you run it, it turns out that everything is the opposite - the lowercase letter is now more capitalized.
The sort command and string comparison in a script fileIn comparison commands capital letters are less than lowercase. String comparisons are performed here by comparing ASCII character codes, the sort order is thus dependent on the character codes.
The
sort
command, in turn, uses the sort order specified in the system language settings.
File checks
Perhaps the following commands are used in bash scripts most often. They allow you to check various conditions concerning files. Here is a list of these commands.
-d file
Checks if a file exists and is a directory.
-e file
Checks if the file exists.
-f file
Checks if a file exists and is a file.
-r file
Checks if a file exists and is readable.
-s file
Check if the file exists and is not empty.
-w file
Checks if a file exists and is writable.
-x file
Checks if the file exists and is executable.
file1 -nt file2
Checks if file1
newer than file2
.
file1 -ot file2
Checks whether file1
older than file2
.
-O file
Checks if the file exists and whether the current user owns it.
-G file
Checks if a file exists and if its group ID matches the current user's group ID.
These commands, as well as many others reviewed today, are easy to remember. Their names, being abbreviations of various words, directly indicate the checks they perform.
Let's try one of the teams in practice:
#!/bin/bash mydir=/home/likegeeks if [ -d $mydir ] then echo "The $mydir directory exists" cd $ mydir ls else echo "The $mydir directory does not exist" fi
This script, for an existing directory, will display its contents.
Display directory contentsWe believe that you can experiment with the rest of the teams on your own, they all apply on the same principle.
Results
Today we talked about how to start writing bash scripts and looked at some basic things. In fact, the topic of bash programming is huge. This article is a translation of the first part of a large series of 11 materials. If you want to continue right now - here is a list of the originals of these materials. For convenience, the one that you just read has been included.
- Bash Script Step By Step - here we are talking about how to start creating bash scripts, using variables, describing conditional constructions, calculating, comparing numbers, strings, finding out information about files.
- Bash Scripting Part 2, Bash the awesome - here the features of working with for and while loops are revealed.
- Bash Scripting Part 3, Parameters & options - this material is devoted to command line parameters and keys that can be passed to scripts, work with data that the user enters, and which can be read from files.
- Bash Scripting Part 4, Input & Output - here we are talking about file descriptors and how to work with them, input, output, error streams, output redirection.
- Bash Scripting Part 5, Sighals & Jobs - this material is dedicated to Linux signals, processing them in scripts, and running scripts on a schedule.
- Bash Scripting Part 6, Functions - here you can learn about creating and using functions in scripts, about developing libraries.
- Bash Scripting Part 7, Using sed - This article focuses on working with the sed text stream editor.
- Bash Scripting Part 8, Using awk - this material is dedicated to programming in the awk data processing language.
- Bash Scripting Part 9, Regular Expressions - here you can read about using regular expressions in bash scripts.
- Bash Scripting Part 10, Practical Examples - here are techniques for working with messages that can be sent to users, as well as a method for monitoring a disk.
- Bash Scripting Part 11, Expect Command - this material is dedicated to the Expect tool, with which you can automate interaction with interactive utilities. In particular, here we are talking about expect-scripts and their interaction with bash-scripts and other programs.
We believe that one of the valuable properties of this series of articles is that, starting from the simplest, suitable for users of any level, gradually leads to quite serious topics, giving everyone a chance to make progress in creating Linux command line scripts.
Dear readers! We ask the guru of bash-programming to tell about how they got to the heights of excellence, to share secrets, and from those who have just written their first script, we are waiting for impressions.
