Basics of creating Bash scripts
Introduction
Before reading this article make sure that you are familiar with the content of the article «Bash Basics»
Basics
At the beginning of the script you need to specify #!/bin/bash and preferably some comment about the purpose of the script
#!/bin/bash
# Example comment
Variables
You can create a variable in a Bash script as follows
VAR=a echo $VAR
a
You can use quotes
VAR="b" echo $VAR
b
VAR="c" echo "$VAR"
c
Assign the value of one variable to another
VAR="c" echo "$VAR" VAR0="$VAR" echo "$VAR0"
c
c
If the first variable is undefined, there will be no error, but nothing will be assigned either
VAR1="$UNKNOWN" echo $VAR1
Of course, it would be nice to get messages about undeclared variables.
:-
${parameter:-word}
 : If parameter is unset or null, the expansion of word is substituted.
 : Otherwise, the value of parameter is substituted.
With :- you can set a default value. That is, if the first variable
is not defined, this value will be assigned.
It is good to use when you need to assign a value to a variable, but only if
it has not already been assigned before.
VAR1="value" NEW1=${VAR1:-text} NEW2=${VAR2:-text} echo "NEW1 = $NEW1" echo "NEW2 = $NEW2" echo "VAR1 = $VAR1" echo "VAR2 = $VAR2"
NEW1 = value NEW2 = text VAR1 = value VAR2 =
In the first case the variable existed, and in the second the variable VAR2 could not be found and
the value text was assigned.
Note that the variables VAR1 and VAR2 themselves have not changed
:=
${parameter:=word}
If parameter is unset or null, the expansion of word is assigned to parameter.
The value of parameter is then substituted.
Positional parameters and special parameters may not be assigned to in this way.
:= behaves similarly to :- but it also assigns the value to the variable that was not set
VAR1="value" NEW10=${VAR1:=foo} NEW20=${VAR2:=foo} echo "NEW10 = $NEW10" echo "NEW20 = $NEW20" echo "VAR1 = $VAR1" echo "VAR2 = $VAR2"
NEW10 = value NEW20 = foo VAR1 = value VAR2 = foo
You can see that foo was used not only to set NEW20 but also for VAR2 which was not defined
How to call a script
First of all you need to make sure that the file
script_1
is executable
If you are in the same directory as
script_1
run
ls -la
-rw-rw-r--. 1 andrei andrei 5252 Nov 28 01:07 script_1
If the fourth character is - then the file is not executable.
You can make it executable with the command
chmod u+x script_1
ls -la
-rwxrw-r--. 1 andrei andrei 5252 Nov 28 01:07 script_1
Note the -rwx, now the fourth character is x
In my
CentOS
highlighting of executable files is configured, so it is green now
Now you can run the script with the command
./script_1
This particular script should accept command line arguments , so we will run
./script_1 word1 word2
word1 will be added to
log.txt
but word2 will not, because word1 is the first argument and word2 is the second one.
To write absolutely all arguments, replace $1 with $*
Let's add the time of command execution to the log (details
here)
#!/bin/bash # Reading from the terminal and writing to the log echo $(date + "%Y-%m-%d %H:%M:%S,%3N") $* << ~/log.txt
./script_1 one two three
cat log.txt
2026-11-28 01:07:33,404 one two three
How to call a script from another script
Suppose from the script parent.sh you need to call the script child.sh
#!/bin/bash
# parent
bash ./child.sh
This is if they are in the same directory, if in different ones - write the path to the script
How to name a script
You can check whether the name is taken by the system with the type command.
For example, you decided to name your script test
type test
test is a shell builtin
That is, the name is taken, you can of course use it, but there will be additional difficulties with calling it.
Command output into a variable
It is often necessary to run a command from a script and assign the result to a variable.
To do this use the syntax
VARIABLE=$(COMMAND)
For example, you need to analyze a log for the number of errors and warnings
#!/bin/bash
ERROR=$(grep -o -i ERROR 2026-11-28-log.txt | wc -l)
WARNING=$(grep -o -i WARNING 2026-11-28-log.txt | wc -l)
echo "Errors: $ERROR; Warnings: $ WARNING"
wc is a command that counts the number of words
Command line arguments
A script can accept arguments from the command line
The first argument is denoted as $1, the second as $2 and so on.
Suppose we want to call a script from the file script_1 and pass some variable there, and then write it to the log
#!/bin/bash # Reading from the terminal and writing to the log echo $1 << ~/log.txt
echo $1 means that we take the first argument
~/ means that the file
log.txt
will be in the home directory
Let's look at the example ar.sh
#!/bin/bash AR=$1 echo $AR $AR $AR ~
This script waits for one external argument and assigns its value to the variable AR
Then the same value is printed three times
./ar.sh hei
hei hei hei
In
C
a similar function is performed by
int argc, char* argv[]
In
Python
-
sys.argv
and
argparse
In
PowerShell
,
you need to list the parameters in advance in the block
param
.
for loop
In its most familiar form the for loop can be written like this
#!/bin/bash for i in `seq 0 9`; do echo $i done
Concatenate strings
String concatenation can be done as follows
concat.sh
#!/bin/bash STR1="heihei" STR2=".ru" STR3=$STR1$STR2 echo $STR3
./concat.sh
heihei.ru
Functions
To avoid writing long commands by hand you can combine them into functions.
For example, you need to swap the screens using
xrandr
this is a rather long command, and in order not to run this command every time after
a reboot you can write a function in
.bashrc
vi ~/.bashrc
function monitors_ { xrandr --output DP-1 --left-of eDP-1 }
Log in to the terminal again.
Now, to swap the screens, it is enough to run
monitors_
Another example - a function inside a regular script, for now it just returns hi
#!/bin/bash export LANG=en_US.UTF-8 say_hi(){ echo "hi" } say_hi
./say_hi
hi
Let's change the code to pass a name to the function and greet with that name
#!/bin/bash export LANG=en_US.UTF-8 say_hi(){ smb=$1 echo "hi "$smb } say_hi Andrei
./say_hi
hi Andrei
An example of a function that changes the image resolution using convert
#!/bin/bash WIDTH=1000 resize_image() { file=$1 EXT="${file##*.}" FULLNAME="${file##/}" echo $FULLNAME if [[ $EXT = "jpg" ]] || [[ $EXT = "png" ]]; then convert $FULLNAME -resize $WIDTH $FULLNAME fi } for file in ./*; do resize_image $file done
#!/bin/bash WIDTH=1000 resize_image() { file=$1 convert $FULLNAME -resize $WIDTH $FULLNAME } convert_to_webp() { file=$1 FILENAME="${file%.*}" FULLNAME_WEBP=$FILENAME".webp" if [[ ! -f $FULLNAME_WEBP ]]; then echo $FULLNAME_WEBP "doesn't exist - converting" cwebp -q 60 $file -o $FULLNAME_WEBP fi } for file in ./*; do EXT="${file##*.}" FULLNAME="${file##/}" echo $FULLNAME if [[ $EXT = "jpg" ]] || [[ $EXT = "png" ]]; then resize_image $file convert_to_webp $file fi done
Checking whether a command exists
You can check whether a command is available or absent using command.
For example, checking for the presence of
firewall-cmd
if ! command -v firewall-cmd &> /dev/null then echo "firewall is not found" exit else echo "firewall is found" exit fi
Arrays
#!/bin/bash arr=(PNG JPG) for item in ${arr[@]} do echo $item done
PNG
JPG
Check whether an array contains a certain element
#!/bin/bash EXT=PNG arr=(PNG JPG) if [[ " ${arr[*]} " =~ " ${EXT} " ]]; then echo "Found" fi
Found
Article author: Andrei Olegovich