Solved

Linux, Scripting with environment

Posted on 2013-12-20
3
331 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
Comment Utility
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
Comment Utility
"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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Daily system administration tasks often require administrators to connect remote systems. But allowing these remote systems to accept passwords makes these systems vulnerable to the risk of brute-force password guessing attacks. Furthermore there ar…
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,…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now