Thursday, October 17, 2024

Nested Variable Substitution and Predefined BASH Variables in Linux – Part 11

 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.

avi@localhost:~$ declare -rx Linux_best_website="www.tecmint.com"

Check the value of variable stored.

avi@localhost:~$ printf "%s" "$Linux_best_website" 

www.tecmint.com

Now declare another variable which again is Read-Only and Executable.

avi@localhost:~$ 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?

avi@localhost:~$ 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.

avi@localhost:~$ 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.

avi@localhost:~/Desktop$ declare x=10

Check the value of variable x, we just defined.

avi@localhost:~/Desktop$ echo $yx

x10

Declare a variable y, the value of which is x.

avi@localhost:~/Desktop$ declare y=x

Check the value of variable y, we just defined.

avi@localhost:~/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.

avi@localhost:~/Desktop$ eval y=$x

Now check the Value of variable ‘y‘.

avi@localhost:~/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.

No.BASH VARIABLERESULT
1auto_resumeProcess command completion for the stopped jobs.
2BASHPATH of the shell.
3BASH_ENVShows the name of the profile file
4BASH_VERSIONShows the version of Bash
5BASH_VERSINFOShows Detailed version information.
6BASH_VERSINFO[0]The major version number (the release).
7BASH_VERSINFO[1]The minor version number (the version).
8BASH_VERSINFO[2]The patch level.
9BASH_VERSINFO[3]The build version.
10BASH_VERSINFO[4]The release status (for example, beta1 ).
11BASH_VERSINFO[5]The value of MACHTYPE .
12CDPATHList of directories separated by colon.
13COLUMNSNumber of Characters per line on Standard Output.
14EUIDUser ID of the current user.
15FCEDITThe default text editor for the fc command.
16FUNCNAMEThe name of the fun
17GROUPSGroups of which the user is a Member.
18HISTFILEThe file containing the command history.
19HOMEThe name of your home directory.
20LINESNumber of horizontal lines on Standard Output.
21MAILName of a file to check for incoming mail
22OSTYPEName of the operating system.
23OLDPWDThe previous working directory
24PWDThe current working directory
25RANDOMPrints a random number
26SHELLThe preferred shell to use
27TIMEFORMATThe format for the time command.
28UIDThe 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.

No comments:

Post a Comment

HTTP Appache Server LAB 7

 Apache HTTP Server (httpd) Configuration,