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

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.

Nested Variables in Shell Scripting
Nested Variables in Shell Scripting

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 VARIABLE RESULT
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[0] The major version number (the release).
7 BASH_VERSINFO[1] The minor version number (the version).
8 BASH_VERSINFO[2] The patch level.
9 BASH_VERSINFO[3] The build version.
10 BASH_VERSINFO[4] The release status (for example, beta1 ).
11 BASH_VERSINFO[5] 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 MAIL 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.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Ronav Saive
A Passionate GNU/Linux Enthusiast and Software Developer with over a decade in the field of Linux and Open Source technologies.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

4 thoughts on “Nested Variable Substitution and Predefined BASH Variables in Linux – Part 11”

    • Thanks for your feedback.

      But Let me know – What in case of multiple variable substitution, where we are dealing with lots of variable and 100’s of lines of code???
      ‘eval’ command comes to rescue there.

      Reply
      • “What in case of multiple variable substitution, where we are dealing with lots of variable and 100’s of lines of code???”

        Unfortunately you did not provide an example of multiple variables. How are we newbies to learn if we are shown only the most basic usage?

        Doesn’t the '-x' switch mean EXPORTABLE, not Executable as explained in Part 10 of these series?

        Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.