Skip to content

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"
or

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"
Using curly braces helps in manipulating the value of the variable for the desired output.

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 $?
Return Codes
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*}
This will give a list of shell variables that start with 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}
This will match the pattern at beginning of the value and replace it with the 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,}
This will change the first character to lowercase

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
This will make it so that only integers can be assigned to myvar.

declare -l myvar
This will make it so that only lowercase characters can be assigned to myvar.

declare -u myvar
This will make it so that only uppercase characters can be assigned to 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
This will remove the restrictions on 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.

Associative Arrays