In shell script there is no need to declare a variable before using it. A variable is a name given to memory location, which is used to store a value. The value can be accessed by variable name for further manipulation.
In this chapter we shall look at below topics:
2.1 How to set a variable?
2.2 How to access a shell variable?
2.3 Rules for defining a variable
2.4 Unsettling a variable
2.5 How to declare a read-only variable?
2.6 Environment variables
2.7 Shell Script arrays
2.1 How to set a variable?
Syntax:
<variable_name>=<value>
Example:
website_name=prodevelopertutorial.com
NOTE:
While setting a variable, there should not be any space between equals “=”. If there is a space, interpreter will throw an error.
Here “website_name” will hold the value “prodevelopertutorial.com”.
2.2 How to access a shell variable?
To access a variable, prefix the name with a “$” dollar symbol.
Example:
$website_name
Note:
If the string value you are assigning contains space, then it should be enclosed in single[”] or double [“”] quotes.
Example:
website_name=" prodevelopertutorial.com is a useful website"
or
website_name=' prodevelopertutorial.com is a useful website'
2.3 Rules for defining a variable:
1. Variable name can have alphanumeric characters.
2. Other than underscore (_), no other special characters are allowed in variable name.
3. A variable name can start with an alphabet or an underscore. It cannot start with number.
2.4 Unsettling a variable:
To unset the variable value, use “unset” keyword.
Syntax:
unset <variable_name>
Example:
unset website_name
2.5 How to declare a read-only variable?
To make a variable value to be read-only use “readonly” keyword. Once you make a variable read only, the value cannot be changed.
Syntax:
readonly <variable_name>
Example:
I have created “read_only.sh” file with below content:
#!/bin/bash website_name=prodevelopertutorial.com readonly website_name echo $website_name unset website_name
Output:
When I try to run it below error will be generated:
prodevelopertutorial.com ./read_only.sh: line 9: unset: website_name: cannot unset: readonly variable
2.6 Environment variables:
Till now we have seen Shell variables. These shell variables are local to the currently running shell. But in Linux like operating system, there exist a special variable that are available system wide, where the programs executing in shell can get the compile time information about its environment. These kind of variables are called as environment variables.
“printenv” command is used in shell to get the information about environment variables. Below is the output according to my system.
SHELL=/bin/bash HISTSIZE=1000 USERNAME=root MAIL=/var/spool/mail/root PATH=/usr/local/sbin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin PWD=/usr/local/shell_script LANG=en_US.UTF-8 HOME=/root SUDO_COMMAND=/bin/bash SHLVL=2 LOGNAME=root
To access these variables, you can directly use
echo $<envt_variable_name>
Example:
echo $SHELL
Output:
/bin/bash
To make a shell variable available as an Environment Variable, use “export <variable_name>” command. Without dollar sign.
Example:
website_name=prodevelopertutorial.com export website_name
2.7 Shell Script arrays
In this chapter we shall understand how to use arrays.
Bash support one dimension arrays. An array is a sequence of elements. Like in other languages, array index starts from 0.
Example of array:
#! /bin/bash # To declare Array array=('Pro' 'Developer' 'Tutorial' 365 'days') # Print all elements of array echo ${array[@]} echo ${array[*]} #Print first element of the array echo ${array[0]} echo ${array} #print any particular element echo ${array[4]} echo ${array[2]}
Output:
Pro Developer Tutorial 365 days Pro Developer Tutorial 365 days Pro Pro days Tutorial