Solved

Linux, Scripting with environment

Posted on 2013-12-20
3
335 Views
Last Modified: 2013-12-20
I am using RHEL 6.5

This takes place on a single client only, no scp or ssh

I get the following:

[root@xxclnt2 home]# cat test
set | wc -l
[root@xxclnt2 home]#
[root@xxclnt2 home]# ./test
53
[root@xxclnt2 home]#
[root@xxclnt2 home]# set | wc -l
66

Open in new window


So putting the "set | wc -l" inside of the script yields less environment variables than performing the same command outside of a script

Why is that.

Is there any way to get the full environment within the script 'test'

Thanks
0
Comment
Question by:Los Angeles1
3 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 39732537
Some variables are not exported to subshells.

You can try this:
cat - > test.sh <<EOF
set > vars-in-script.txt
EOF
bash test.sh
set > vars-in-shell.txt
diff vars-in-*

Open in new window


I get differences like COLUMNS=80 / HISTFILE / HISTFILESIZE / HISTSIZE / LINES=24 / PS1 / PS2

If you want the complete set of variables don't execute the script with ./test or bash test / sh test but source it:
# source test
or shorter
# . test
In the latter case no subshell is created, the lines are executed within your shell.
0
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39732551
"set" displays all variables and functions, whether the variables are exported to the environment or not - but only variables exported to the environment are inherited by a script.

"printenv" or "env", on the other hand,  display only variables which are part of the environment.
Run your script with "printenv" instead of "set" and compare the result with "printenv | wc -l" from the command prompt.
You will notice that there's no difference.

So it seems that your initialization file(s) contain variable assignments or function definitions which do not get "exported" to the environment. The shell itself also sets some "unexported" variables.
0
 
LVL 9

Expert Comment

by:gt2847c
ID: 39732559
To add to the above, the "export" built-in bash function will show you which environment variables are marked for export to sub-shells and scripts.
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question