?
Solved

Linux, Scripting with environment

Posted on 2013-12-20
3
Medium Priority
?
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Fine Tune your automatic Updates for Ubuntu / Debian
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.
Suggested Courses
Course of the Month13 days, 10 hours left to enroll

800 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