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.

[email protected]:~$ declare -rx Linux_best_website=""

Check the value of variable stored.

[email protected]:~$ printf "%s" "$Linux_best_website"

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

[email protected]:~$ declare -rx Linux_website="Linux_best_website"

Now the situation is, we have defined two variables.

‘Linux_best_website’, the value of which is “”
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 “”. So the final output of running the below command should be.

[email protected]:~$ printf "%s" "$Linux_website"

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


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 


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 


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[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 liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

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.

      • “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?


Leave a Reply to dragonmouth Cancel reply

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.