Types of variables in shell scripting with examples

Getting started

Hi all, I am Karthick from TechieDrone. Today I am going to share basic things that you should be aware to gain decent knowledge on shell scripting. Let’s dive in to the topic 😎 In this article, I am going to start with how to define variables in bash scripting and what are the methods to define variables with examples.

Constant variable through out the script

Simply declare a pre-defined variable globally like VariableName=YourVariableHere which will never change throughout the execution. For example, I am assigning A=1, B=2, C=$A. Here their value will be constant through out the execution. No matter how many times you run this script i.e., their value does not change 😀

Example:

1
2
3
#!/bin/bash
A=1;B=2;C=$A
echo " a=$A \n b=$B \n c=$C"

Result:

1
2
3
 a=1
 b=2
 c=1

Capture the output of something into a variable

In this method I am going to show how to assign a value of something into a variable with example. Say I am going to display formatted date as I need using bash script. Lets see how to achieve it. In this example, I’m going to show you two different workaround to capture the output of something into a variable. I will explain the difference between them in detail in later session 🙂

Example:

1
2
3
#!/bin/bash
customDate01=`date +%F`;customDate02=$(date +%D)
echo " formatted date 01 : $customDate01 \n formatted date 02 : $customDate02"

Result:

1
2
 formatted date 01 : 2018-03-03
 formatted date 02 : 03/03/18

Assign run-time variable without prompt

It is nothing but getting the inputs for the variables in run-time. i.e., getting inputs at the run-time. Lets see how to do it 😉 Say I am going put below codes into a file named workaround-variables-eg3.sh. In this example I have assigned value for A & B during the execution (refer script execution part)

Example:

1
2
3
4
#!/bin/bash
A=$1;B=$2
C=`expr $A + $B`
echo "Addition of $A and $B is $C"

Script execution:

1
sh workaround-variables-eg3.sh 1 3

Result:

1
Addition of 1 and 3 is 4

Assign run-time variable with prompt

In above example, we assign variables in run-time without being prompt. i.e., We declared the value for A & B while executing the code. Here in this example, I am going to show how to get the inputs by displaying user friendly messages 💡 Say I am going put below codes into a file named workaround-variables-eg4.sh. In this example I have displayed some messages to get the input value for A & B during the execution (refer script execution part)

Example:

1
2
3
4
5
#!/bin/bash
read -p "Enter the value for A : " A
read -p "Enter the value for B : " B
C=`expr $A + $B`
echo "Addition of $A and $B is $C"

Script execution:

1
2
3
sh workaround-variables-eg4.sh
Enter the value for A : 2
Enter the value for B : 4

Result:

1
Addition of 2 and 4 is 6

That’s all for now 😎 Still these concepts looks messy for you? No worries, feel free to ask us through comment section 🙂

Spread the word :)

Leave a Reply

Be the First to Comment!

avatar
  Subscribe  
Notify of