Bash Script Tutorial: Introduction to Variables

command-line-blogImage
A shell script allows you to set and use your own variables within the script. Setting variables allows you to temporarily store data and use it throughout the script. This variables are called user variables in bash.
key points on variables!
User variables can be any text string of up to 20 letters, digits, or an underscore character.
User variables are case sensitive, so the variable Var1 is different from the variable var1 .
Values are assigned to user variables using an equal sign.
No spaces can appear between the variable, the equal sign, and the value
variable=”John Smith”

The shell script automatically determines the data type used for the variable value. So the user doesn’t have to declare variable type like in most programming languages eg: int number=1, or string name=”George”
Variables defined within the shell script maintain their values throughout the life of the shell script but are deleted when the shell script completes.
In programming there are two types of naming convention: Camel Case notation “varName” and Underscore notation “var_name”
declaring variables in bash (same for number, char, or string)

number=10  #declaring a number
age=44	        #declaring a number
first_name=john  #declaring a string
last_name=smith #declaring a string
char=c		#declaring a characters

When access the value store in a variable, we use the “echo” command. And we precede the name of the variable by appending the dollar sing. This is the way bash references the value store in memory.

#displaying messages
echo $number
echo $first_name

using variable inside messages

echo “my name is $first_name $last_nameecho$first_name $last_name is $age years old”

setting default variables. In this example we declare a variable and if variable is null set it to 10

#setting default value to 10
var=${var:-10} 
#setting default value to “john smith”
echo ${name:=”john smith”}

Using command substitution to assign the output of a command to a variable. This is a common use of command substitution in bash when creating variables. In this example, we assign the output of the “date” command to “todaysDate” variable. And in the second example, we assign the output of “du -hs” to “totalSize” variable, which will hold the size of the hard drive.

todaysDate=$(date +%F)
totalSize=$(du -hs)

Another type of variables we should be familiar with, are system variables. This variables are defined by the system. To see a list of all system variable we use the “printenv” command.

printenv

I wanted to finish this tutorial with a couple of challenges, that will help you gaining proficiency with the bash.
challenges
1-Use command substitution to get the total number of files inside your current directory. Then use the echo command to display the value to the terminal.
2-Use command substitution to get the total size of your current directory and display to terminal using echo command

Share This!

Leave a Reply

Your email address will not be published.