Solved

Linux, Scripting with environment

Posted on 2013-12-20
3
337 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AWS ELB 5 95
Run same command on multiple files in Linux 3 54
cannot rename datastore 3 69
how to include conditional log rotate in liunx. 17 25
Fine Tune your automatic Updates for Ubuntu / Debian
Google Drive is extremely cheap offsite storage, and it's even possible to get extra storage for free for two years.  You can use the free account 15GB, and if you have an Android device..when you install Google Drive for the first time it will give…
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…
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.

809 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