LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-19-2017, 04:12 PM   #1
krishnar
Member
 
Registered: May 2016
Posts: 32

Rep: Reputation: Disabled
Export command


Hi All,

I was trying to learn how export command works.

I tried to export a vaiable as show below:

[ec2-user@Splunk-Server ~]$ test=a
[ec2-user@Splunk-Server ~]$ echo $test
a
[ec2-user@Splunk-Server ~]$ export test --> Exported a variable

[ec2-user@Splunk-Server ~]$
[ec2-user@Splunk-Server ~]$
[ec2-user@Splunk-Server ~]$ sudo su - --> I changed to root login.
Last login: Thu Jan 19 22:07:26 UTC 2017 on pts/0

[root@Splunk-Server ~]# echo $test

[root@Splunk-Server ~]#

I can't get that variable in root login though I exported that variable.

Am I missing something here?

Krishna
 
Old 01-19-2017, 05:06 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 23,815

Rep: Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974Reputation: 6974
Quote:
Originally Posted by krishnar View Post
Hi All,
I was trying to learn how export command works. I tried to export a vaiable as show below:

[ec2-user@Splunk-Server ~]$ test=a
[ec2-user@Splunk-Server ~]$ echo $test
a
[ec2-user@Splunk-Server ~]$ export test --> Exported a variable

[ec2-user@Splunk-Server ~]$
[ec2-user@Splunk-Server ~]$
[ec2-user@Splunk-Server ~]$ sudo su - --> I changed to root login.
Last login: Thu Jan 19 22:07:26 UTC 2017 on pts/0

[root@Splunk-Server ~]# echo $test

[root@Splunk-Server ~]#

I can't get that variable in root login though I exported that variable. Am I missing something here?
Aside from the very obvious "you changed user ID's and the variable was only exported to ec2-user"? No....

When you change users, the variables you assign to a user stay with that user. When you started a new root shell, those variables don't apply unless you set them in the root .profile/.bash_rc or in the default /etc/profile.
 
Old 01-19-2017, 06:02 PM   #3
designator
Member
 
Registered: Jun 2003
Location: California, USA
Distribution: OpenSUSE Tumbleweed
Posts: 219

Rep: Reputation: 37
I'm not sure that's obvious especially to someone trying to understand what exactly export command does.

A good explanation is here:
http://unix.stackexchange.com/questi...variables-in-b
 
1 members found this post helpful.
Old 01-19-2017, 06:39 PM   #4
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 4,547

Rep: Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082Reputation: 2082
The issue is what the "su -" command does. From the manpage (emphasis mine):
-, -l, --login
make the shell a login shell, clears all envvars except for TERM, initializes HOME, SHELL, USER, LOGNAME and PATH
The sudo command also clears the environment unless you use the "-E" option to request that it not do so.
 
1 members found this post helpful.
Old 01-19-2017, 07:16 PM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,940

Rep: Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780
also the op should be made aware that the exported vars go down the chain and not up, eg if you export a variable any child process created will have acces to it but parent processes will not, you cant for instance export a variable to a child process, have that child process alter the variable and then export it back up to the parent process.

Which is a bit of a 'gotcha' when using pipes, got me when I was learning anyway!

Last edited by Keith Hedger; 01-19-2017 at 07:17 PM.
 
1 members found this post helpful.
Old 01-20-2017, 02:40 AM   #6
nodir
Member
 
Registered: May 2016
Posts: 222

Rep: Reputation: Disabled
on a side note: test is not a good name for a variable or a script or such, as it is already a shell buildin.
 
2 members found this post helpful.
Old 01-20-2017, 05:44 AM   #7
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,940

Rep: Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780Reputation: 780
Quote:
Originally Posted by nodir View Post
on a side note: test is not a good name for a variable or a script or such, as it is already a shell buildin.
well spotted
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
export command help ? Ribo01 Linux - Newbie 2 04-19-2011 08:39 AM
export command brgsousa Linux - Software 1 03-20-2010 07:10 PM
[SOLVED] export command deostroll Linux - Newbie 5 02-20-2010 06:55 AM
export command rohr Mandriva 1 04-11-2005 05:24 AM
Export Command choawk Linux - General 1 06-06-2003 01:26 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 05:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration