Bash Shell Script

Bash Shell Script

·

4 min read

#90 Days DevOps Challenge#Zero To Hero#Day6 💻 Challenge Accepted with #tws by Shubham Londhe 👨‍🏫

90 Days DevOps Challange@ Zero To Hero Journey # Day 1

  1. First you need to find out where is your Bash interpreter located. Enter the following into your command line:

     $ which bash
     /bin/bash
    

    This command reveals that the Bash shell is stored in /bin/bash. This will come into play momentarily.

  2. The next thing you need to do is open our favorite text editor and create a file called hello_world.sh. We will use nano for this step.

     $ nano hello_world.sh
    
  3. Copy and paste the following lines into the new file:

     #!/bin/bash
     # declare STRING variable
     STRING="Hello World"
     # print variable on a screen
     echo $STRING
    

    NOTE: Every bash shell script in this tutorial starts with a shebang: #! which is not read as a comment. First line is also a place where you put your interpreter which is in this case: /bin/bash.

  4. Navigate to the directory where your hello_world.sh script is located and make the file executable:

     $ chmod +x hello_world.sh
    
  5. Now you are ready to execute your first bash script:

     $ ./hello_world.sh
    

    The output you receive should simply be:

     Hello World
    

Simple Backup bash shell script

When writing a Bash script, you are basically putting into it the same commands that you could execute directly on the command line. A perfect example of this is the following script:

#!/bin/bash
tar -czf myhome_directory.tar.gz /home/linuxconfig

This will create a compressed tar file of the home directory for user linuxconfig. The tar command we use in the script could easily just be executed directly on the command line.

So, what’s the advantage of the script? Well, it allows us to quickly call this command without having to remember it or type it every time. We could also easily expand the script later on to be more complex.

Variables in Bash scripts

In this example we declare simple bash variable $STRING and print it on the screen (stdout) with echo command.

#!/bin/bash
STRING="HELLO WORLD!!!"
echo $STRING

The result when we execute the script:

$ ./hello_world.sh
HELLO WORLD!!!

Circling back to our backup script example, let’s use a variable to name our backup file and put a time stamp in the file name by using the date command.

#!/bin/bash
OF=myhome_directory_$(date +%Y%m%d).tar.gz
tar -czf $OF /home/linuxconfig

The result of executing the script:

$ ./backup.sh
$ ls
myhome_directory_$(date +20220209).tar.gz

Now, when we see the file, we can quickly determine that the backup was performed on February 9, 2022.

Global vs. Local variables

In Bash scripting, a global variable is a variable that can be used anywhere inside the script. A local variable will only be used within the function that it is declared in. Check out the example below where we declare both a global variable and local variable. We’ve made some comments in the script to make it a little easier to digest.

#!/bin/bash
# Define bash global variable
# This variable is global and can be used anywhere in this bash script
VAR="global variable"

function bash {
# Define bash local variable
# This variable is local to bash function only
local VAR="local variable"
echo $VAR
}

echo $VAR
bash
# Note the bash global variable did not change
# "local" is bash reserved word
echo $VAR

The result of executing this script:

$ ./variables.sh
global variable
local variable
global variable

Passing arguments to the bash script

When executing a Bash script, it is possible to pass arguments to it in your command. As you can see in the example below, there are multiple ways that a Bash script can interact with the arguments we provide.

#!/bin/bash
# use predefined variables to access passed arguments
#echo arguments to the shell
echo $1 $2 $3 ' -> echo $1 $2 $3'

# We can also store arguments from bash command line in special array
args=("$@")
#echo arguments to the shell
echo ${args[0]} ${args[1]} ${args[2]} ' -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}'

#use $@ to print out all arguments at once
echo $@ ' -> echo $@'

# use $# variable to print out
# number of arguments passed to the bash script
echo Number of arguments passed: $# ' -> echo Number of arguments passed: $#'

Let’s try executing this script and providing three arguments.

$ ./arguments.sh Bash Scripting Tutorial

The results when we execute this script:

Bash Scripting Tutorial  -> echo $1 $2 $3
Bash Scripting Tutorial  -> args=("$@"); echo ${args[0]} ${args[1]} ${args[2]}
Bash Scripting Tutorial  -> echo $@
Number of arguments passed: 3  -> echo Number of arguments passed: $#

Subscribe to my newsletter

Read articles from directly inside your inbox. Subscribe to the newsletter Akash Raj and don't miss out.

SUBSCRIBE Akash Raj

DevOps articles

©2023 Akash Raj @dreamdevopsakash

Archive·Privacy policy·Terms

Publish with Hashnode

Powered by Hashnode - Home for tech writers and readers