Variables
A symbolic name for a chunk of memory to which we can assign values, read and manipulate its contents. A variable in bash can contain a number, a character, a string of characters. Variable in Bash dose not have data types. You have no need to declare a variable, just assigning a value to it.
How to create and access a variable
VAR=Value
Example:
name=amir
age=30
Note that there must be no spaces around the “=” sign. for example, VAR = value doesn’t work.
There are two ways to access a variable:
1- Using “$ symbol” followed by the variable name
$echo $name
2- Using the “$ symbol” followed by curly braces and variable name inside the curly braces.
$echo ${name}
Also, you can run any bash commands and store the output of the command to the variable. You must run the command within $() like below and later use it in your script.
amir@server:~$Today=$(date)
amir@server:~$echo ${Today}
Mon May 2 04:59:59 PM UTC 2022
Removing the variable
You can remove the variable using the unset command.
The Unset command removes the values and attributes of variables and functions.
unset var_name
For example: unset Today (Today is a variable that we define in last example)
amir@server:~$unset Today
Read-only variable
A read-only state means you cannot change the value or remove the variable once it is created. There are two ways to create the read-only variable.
Use bash built-in readonly command to create the read-only variable.
amir@server:~$readonly site="SMENODE"
Alternatively, you can use the declare command with the -r flag.
amir@server:~$declare -r site="SMENODE"
You cannot reassign or unset the readonly variable value, it will throw an error.
Bash built-in special variables
Bash has some special built-in variables.
$0 = Script name.
$# = Count the length of values
$? = Stores exit status of last ran the command
$$ = Stores process ID for your bash session
$0 = Script name
$1 .. $9 = Arguments passed to the script
$# = Count the length of values
$@, $* = Stores array of all values
In below you can see one example of these variables
- Length of a value ($#)
Using $#, we can find total number of arguments passed or count the length of any values.
amir@server:~$country="Canada"
amir@server:~$echo ${#country}
6
Environment variable
These variables are loaded when you start your bash sessions, and they are maintained by bash.
Keep in mind that your variable is bound only to your current session and if we want to make it persistent we have to put it in ~/.bashrc or ~/.bash_profile.
For example, we can change default text editor of Linux.
You can get the list of environmental variables by running the “printenv” command. environmental variables are defined with capital letters.
amir@server:~$printenv
Export command : for define a variable that we can access anywhere we can use export keyword before the variable name. Actually it means that defined variable is not limited to current shell an we can use variable in other scripts.
export variable=value
#############script1################
#!/bin/bash
country=usa
city=Manhattan
echo country : $country, city : $city
export country
./script2.sh
#############script2################
#!/bin/bash
city=LA
#we can use exported variable from scipt1
echo country : $country, city : $city
#############result################
country : usa, city : Manhattan
country : usa, city : LA
Quoting
- The double quote “ ”
it protects everything enclosed between two double quote marks except $, ‘, ” and .
amir@server:~$echo "$SHELL"
/bin/bash
# In this example override the role of next character and treat $ as a regular character
amir@server:~$echo "$SHELL"
$SHELL
- The single quote ‘ ‘
it protects everything enclosed between two single quote marks. Special character dose not have any effect on it.
amir@server:~$echo '$SHELL'
$SHELL
- Back Quotes ` `
The back quotes allow us to execute the commands inside a shell script.
amir@server:~$echo `date`