Shell Variables
Shell Variables (Part 1)
Creating Shell Variables
Exporting Shell Variables
Exporting a variable makes it available to the parent shell as well as the children shell.
If a variable is set in the parent shell then it wont be accessible in the child shell. To make the value of the variable available in the child shell , exporting is used.
Note
Any changes made in the child shell to an exported variable (from parent shell) will not be reflected in the parent shell.
export myvar="value string"
myvar="value string"
export myvar
Using Variable Values
The value of the variable is displayed using the echo
command.
echo $myvar
echo ${myvar}
echo "${myvar}_something"
Removing a variable
unset myvar
The value of the variable can also be removed by doing
myvar=
Note : There is nothing after the
=
sign in the above command.
Testing if a variable is set
[[ -v myvar ]];
echo $?
0 : success (variable is set)
1 : failure (variable is not set)
Note
echo $?
returns the exit code of the last executed command.
A status code of 0 means that the command was executed successfully
and a stuatus code of non-zero value means that the command resulted in
an error or failed to execute.
Testing if a variable is not set
[[ -z ${myvar+x} ]];
echo $?
Here x
can be any string.
Return Codes
0 : success (variable is not set)
1 : failure (variable is set)
Substitue default value
If the variable is not set, display "bananas" as its default value
echo ${myvar:-"bananas"}
If the variable is not set, change its value to "bananas" and display its value as "bananas".
echo ${myvar:="bananas"}
Reset value if variable is set
If the variable myvar
is set, then set "wutermellon"
as its value
echo ${myvar:+"wutermellon"}
List of variable names
echo ${!H*}
H
.
Length of (string) value of variable
echo ${#myvar}
If myvar
is not set then it displays 0
.
Slice of (string) value of variable
echo ${myvar:5:4}
This will display 4 characters of the value and
will also skip the first 5 characters of the value.
Here 5
is the offset and 4
is the slice length.
The offset can also be negative. All the characters to the left of the index -2 are ignored.
echo ${myvar: -2:9}
Remove matching pattern
echo ${myvar#pattern}
This will only remove the matched pattern once.
echo ${myvar##pattern}
This will match the pattern maximum number of times and remove them.
Keep matching pattern
echo ${myvar%pattern}
This will display the matched pattern only once.
echo ${myvar%%pattern}
This will display the matched pattern maximum number of times.
Replace matching pattern
echo ${myvar/pattern/string}
This will match once and replace it with the string.
echo ${myvar//pattern/string}
This will match maximum number of times with the pattern and replace it with the string.
Replace matching pattern by location
echo ${myvar/#pattern/string}
echo ${myvar/%pattern/string}
This will match the pattern at the end of the value and replace it with the string
Changing Case
Changing to Lowercase
echo ${myvar,}
echo ${myvar,,}
This will change all the characters to lowercase.
Changing to Uppercase
echo ${myvar^}
This will change the first character to uppercase.
echo ${myvar^^}
This will chane all the characters to uppercase.
Restricting value types of a variable
declare -i myvar
myvar
.
declare -l myvar
myvar
.
declare -u myvar
myvar
.
declare -r myvar
This will make it so that the variable is read only.
Removing restrictions on types of a variable
declare +i myvar
declare +l myvar
declare +u myvar
myvar
.In the case of read-only restriction , removing it is not possible.
Indexed Arrays
The index of arrays in bash need not be in succession.