Saturday, May 10, 2014

Unix Shell Variables(1)

1. readonly export
readonly: declares the specific variables to be "readonly". From technical perspective, readonly just "export" this variable to the global "readonly" list, readonly -p can be used to checkout the current list in the current context.

export: export the specific variables to be in current environment. Environment means a set of variables which are available globally in the current script context. export -p can be used to checkout the current list of variables (current environment)

script:
 #! /bin/bash  
 MyVariable="Hello world!"  
 export MyVariable  
 export -p | grep "MyVariable"  
 # output: declare -x MyVariable="Hello world!"  

 readonly MyVariable  
 MyVariable="Amazing world!"  
 # output: ./script: line 8: MyVariable: readonly variable  

 echo $MyVariable  
 # output: Hello world!  

 readonly -p | grep "MyVariable"  
 # output: declare -rx MyVariable="Hello world!"  

terminal:
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ ./script  
 declare -x MyVariable="Hello world!"  
 ./script: line 9: MyVariable: readonly variable  
 Hello world!  
 declare -rx MyVariable="Hello world!"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ export -p | grep "MyVariable"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$ readonly -p | grep "MyVariable"  
 aubinxia@aubinxia-fastdev:~/Desktop/xxdev$   

2. env, unset
env -i: used to initialize the environment variable directly. After using this, export -p lists the current environment which contains our newly declared variable "YourVariable". Note: -i means "ignore the inherited environment", my current understanding is: if the variable is the one in current environment(for example, PATH), then we choose to ignore the environment variable, and initialize that variable to the new value in the current environment.
unset is used to remove variables of functions. After using that, the variable of function becomes undefined.

 #! /bin/bash  
 env -i YourVariable="Hello Your world!"  
 export -p | grep "YourVariable"  
 #output: YourVariable=Hello Your world!  

 YourVariable="Your amazing world!"  
 echo $YourVariable  
 unset YourVariable  
 echo $YourVariable  
 #output: <empty line>  

 a=10  
 b=20  
 c=30  
 echo $a, $b, $c  
 #output "10, 20, 30"  

 unset a  
 echo $a, $b, $c  
 #output ", 20, 30"  

3. Variable Expansion
When we refer to one variable, we should use $varname, but if there is any following characters which could also be taken as part of the variable name, we should use {} to separate the variable name and following characters.

In 2nd command, echo $var__amazing world, "var_amazing" is taken as the variable name, which is not defined in this case, so it outputs null.

 #! /bin/bash  
 var="Hello world"  
 echo $var  
 #output: "Hello world"  

 echo $var__amazing world  
 #output: "world                   "  

 echo ${var}__amazing world  
 #output: "Hello world__amazing world"  

Note: when combining the file system command with the variable, we should be extremely careful.
 #! /bin/bash  
 var="xxdev"  
 echo /$var  
 #output "/xxdev"  

 unset var  
 rm -rf /$var  
 #If var is undefined, then it expands to empty string, then this  
 #command will remove everything in the root path!  

No comments:

Post a Comment