Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Linux, Scripting with environment

Posted on 2013-12-20
3
Medium Priority
?
347 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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.
Suggested Courses

604 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