Shell Scripting – Local Variables
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed special characters in the shell.
https://cloudaffaire.com/shell-scripting-special-characters/
In this blog post, we will discuss variables in the shell. Like any other scripting language, the shell also supports variables. A variable, in a nutshell, is a temporary space to store some value. In the shell, you can store either string or integer as a variable value. Variables can be user-defined or system-defined and the scope of the variables can be local or global. In this blog post, we will mainly focus on user-defined local variables.
Shell Scripting – Local Variables
Variable declaration:
There are several ways you can define a variable in the shell but the preferred way is by using declare statement. Variable can also be assigned from user input, from command output, from a file, or from the value of another variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
####################################### ## Shell Scripting | Local Variables ## ####################################### ## Prerequisites: One Unix/Linux/POSIX-compliant operating system with bash shell ## ------------------- ## Variable assignment ## ------------------- ## Variable assignment is straight forward and simple, variable_name=variable_value (value can be anything with correct syntax) ## You can get the variable_value using a doller prifix to variable_name ($variable_name) a=10 b=$ d=debjeet echo $a # returns 10 echo $b # returns $ echo $c # returns debjeet ## Variable can be assigned a value of another variable a=10 b=$a # value of a is assigned to b echo $b # returns 10 ## Variable can be assigned using declare declare -i a=10 declare -a b=(1 2 3 4 5) declare -r c=read_only_can_not_be_changed declare d=debjeet echo $a # returns 10 echo $[b[1]] # returns 1 echo $c # returns read_only_can_not_be_changed echo $d # returns debjeet c=change # error as c is a read only variable ## Variable can be assigned using input from user read -p 'Enter your Name: ' name && echo "hello $name" # returns hello ## Variable can also be assigned from file echo "debjeet" > name.txt read name < ./name.txt echo "hello $name" # returns debjeet ## Variable can be assigned using command substitution name=`cat name.txt` && echo $name # returns debjeet # another way name=$(cat name.txt) && echo $name # returns debjeet ## Variable can be assigned using let command (arithmetic expressions only) let a=10+15 let b=debjeet let "s = (c = 10, d = 20, 20 + 20)" echo $a # returns 25 echo $b # returns 0 echo $s # returns 40 |
Variable substitution:
You can substitute a variable value at any time.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## --------------------- ## Variable substitution ## --------------------- a=10 hello=$a echo hello # returns hello echo $hello # returns 10 echo ${hello} # returns 10 hello="H E L L O" echo $hello # returns HELLOW echo "$hello" # returns H E L L O hello= # empty variable or null variable echo $hello # returns nothing |
Variable types:
Shell only supports the variable of type string or integer. But that does not mean you can not use other types of variables like float or Boolean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
## -------------- ## Variable types ## -------------- ## Variable can be interger and aithmetic operations can be performed on them declare -i a=20 declare -i b=3 echo `expr $a + $b` # returns 23 echo `expr $a - $b` # returns 18 echo `expr $a / $b` # returns 6 echo "$a/$b" | bc -l # returns 6.66666666666666666666 echo `expr $a \* $b` # returns 60 echo `expr $a % $b` # returns 2 ((a++)); echo $a # returns 21 ((b--)); echo $b # returns 2 ## Bash does not support variable of type float but you can perform floting point calculation using bc echo "7.5+3.5" | bc -l # returns 11.0 echo "7.5-3.5" | bc -l # returns 4.0 echo "7.5/3.5" | bc -l # returns 2.14285714285714285714 echo "7.5*3.5" | bc -l # returns 26.25 echo "7.5%3.5" | bc -l # returns .000000000000000000010 ## Bash does not support variable of type boolean but you can use bollean using regular string bool=true if [ "$bool" = true ]; then echo "this is true" fi bool=false if [ "$bool" = false ]; then echo "this is false" fi ## Bash also supports variable of type array with string, char or interger data type myarray=(1 2 3 4 5) # initialize an array for no in "${myarray[@]}" do echo $no # returns 1 2 3 4 5 done ## Variable can be of type string and char (both are interpreted same) msg=hello echo "$msg" # returns hello msg="${msg} world" echo $msg # returns hello world #msg= hello world # does not work due to whitespace msg="hello world"; echo $msg # returns hello world char="a"; echo $char # returns a |
Variable scope:
A local variable is bound with the process that has initialized it. Another process can not view or use the value of a local variable. You can use the export command to make a local variable global. A global variable value can be accessed by any process. You can also use the local command to explicitly define a local variable (available within functions only).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## -------------- ## Variable Scope ## -------------- FOO=a # set "local" environment variable : local BAR=b # explicitly set a local variable in an anonymous function, inbuilt local is only supported in function echo $FOO # 'a' echo $BAR # nothing bash # forks a child process for the new shell echo $FOO # not set exit # return to the original shell echo $FOO # still 'a' export FOO # make FOO an environment variable bash # fork a new "child" shell echo $FOO # outputs 'a' FOO=b # modifies environment (not local) variable bash # fork "grandchild" shell echo $FOO # outputs 'b' exit # back to child shell exit # back to the original shell echo $FOO # outputs 'a' |
Hope you have enjoyed this article. In the next blog post, we will discuss user-defined global variables.