The last two article on BASH Shell, where we discussed variables in detail were highly appreciated among our readers. We as Tecmint-Team are very keen to provide you with Latest, Up-to-date and relevant topics covered in details. Moreover we always try to touch major perspectives of the corresponding topics.
Here is the last article on Linux Variables where we are going to see variables substitution and variables defined in Shell before closing this topic.
Bash performs variable substitution before the command is really executed. The Linux Bash Shell searches for all the ‘$’ sign before executing the command and replace it with the value of variable. The process of Bash Variable substitution is performed only once. What if we have nested variables?
Note: By nested variable we mean, variable declared inside variable. Lets see the above scenario in the example below.
Declare a variable which is Read-Only and Executable as below.
[email protected]:~$ declare -rx Linux_best_website="www.tecmint.com"
Check the value of variable stored.
[email protected]:~$ printf "%s" "$Linux_best_website" www.tecmint.com
Now declare another variable which again is Read-Only and Executable.
[email protected]ocalhost:~$ declare -rx Linux_website="Linux_best_website"
Now the situation is, we have defined two variables.
‘Linux_best_website’, the value of which is “www.tecmint.com”
and, ‘Linux_website’, the value of which is “Linux_best_website”
What would be the result, if we run the below one-line command?
[email protected]:~$ printf "%s" "$Linux_website"
It should first replace the variable ‘$Linux_website‘, with value “Linux_best_website” and then “$Linux_best_website” is again a variable the value of which is “www.tecmint.com”. So the final output of running the below command should be.
[email protected]:~$ printf "%s" "$Linux_website" www.tecmint.com
But unfortunately, this is not the situation, the output we are getting is Linux_best_website.
Reason? Yup! Bash substitute the value of variable only once. What about complex scripts and programs where we need to substitute variables frequently as well as needs to substitute the variable more than once?
Here comes the command ‘eval‘ which performs additional work of variable substitution more than once in a script. Here is an example to make the whole working as clear as glass.
Declare a variable x, the value of which is 10.
[email protected]:~/Desktop$ declare x=10
Check the value of variable x, we just defined.
[email protected]:~/Desktop$ echo $yx x10
Declare a variable y, the value of which is x.
[email protected]:~/Desktop$ declare y=x
Check the value of variable y, we just defined.
[email protected]:~/Desktop$ echo $y x
Here is the problem of BASH one time variable substitution, which don’t performs an extra round of variable substitution. We are using ‘eval‘ command to fix this.
[email protected]:~/Desktop$ eval y=$x
Now check the Value of variable ‘y‘.
[email protected]:~/Desktop$ echo $y 10
Hurrah! The issue was fixed and ‘eval‘ command won the race :)
Not to mention, ‘eval‘ command is very helpful in large script programs and is a very handy tool.
The last but not the least part of this post is BASH predefined variables. No! Don’t get panic seeing this list. You never need to remember the whole list before you start writing scripts except a few. As a part of learning process, we are presenting the BASH predefined variable List.
|1||auto_resume||Process command completion for the stopped jobs.|
|2||BASH||PATH of the shell.|
|3||BASH_ENV||Shows the name of the profile file|
|4||BASH_VERSION||Shows the version of Bash|
|5||BASH_VERSINFO||Shows Detailed version information.|
|6||BASH_VERSINFO||The major version number (the release).|
|7||BASH_VERSINFO||The minor version number (the version).|
|8||BASH_VERSINFO||The patch level.|
|9||BASH_VERSINFO||The build version.|
|10||BASH_VERSINFO||The release status (for example, beta1 ).|
|11||BASH_VERSINFO||The value of MACHTYPE .|
|12||CDPATH||List of directories separated by colon.|
|13||COLUMNS||Number of Characters per line on Standard Output.|
|14||EUID||User ID of the current user.|
|15||FCEDIT||The default text editor for the fc command.|
|16||FUNCNAME||The name of the fun|
|17||GROUPS||Groups of which the user is a Member.|
|18||HISTFILE||The file containing the command history.|
|19||HOME||The name of your home directory.|
|20||LINES||Number of horizontal lines on Standard Output.|
|21||Name of a file to check for incoming mail|
|22||OSTYPE||Name of the operating system.|
|23||OLDPWD||The previous working directory|
|24||PWD||The current working directory|
|25||RANDOM||Prints a random number|
|26||SHELL||The preferred shell to use|
|27||TIMEFORMAT||The format for the time command.|
|28||UID||The ID of the current user|
There are a huge list of Predefined BASH Variable. We have tried to produce a list of most frequently used.
That’s all for now. I’ll be here again with another interesting article. Till then stay tuned and connected to TecMint. Don’t forget to provide us with your valuable feedback in the comment section below.