LinuxQuestions.org
Visit Jeremy's Blog.
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 02-09-2011, 11:50 AM   #1
cytmtn
LQ Newbie
 
Registered: Feb 2009
Location: Copenhagen, Denmark
Distribution: ubuntu
Posts: 9

Rep: Reputation: 1
environmental variable in bash command string


Say I do this:

Code:
a@b:~$ export A=hi
a@b:~$ echo $A
hi
a@b:~$ bash -c "export A=blah; echo $A"
hi
a@b:~$
Why doesn't the bash command print the new value of $A? Is there a way to make it do so?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 02-09-2011, 12:17 PM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,551
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by cytmtn View Post
Why doesn't the bash command print the new value of $A? Is there a way to make it do so?
Because bash -c "export A=blah; echo $A" runs export A=blah; echo $A in a sub-process which does not change anything in the parent process.
 
Old 02-09-2011, 12:31 PM   #3
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,365
Blog Entries: 3

Rep: Reputation: 86
If this answers your question, please mark the thread SOLVED.

There are hoops to jump and mountains to climb if you need a child-process to alter
the environment of a parent-process. It is possible but not easy nor recommended.

~~~ 0;-Dan
 
Old 02-09-2011, 01:15 PM   #4
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 329

Rep: Reputation: 140Reputation: 140
Use single quotes instead of double quotes.
$A in double quotes gets expanded to 'hi' before the bash -c command is executed
Code:
bash -c 'export A=blah; echo $A'
blah
 
2 members found this post helpful.
Old 02-09-2011, 01:56 PM   #5
cytmtn
LQ Newbie
 
Registered: Feb 2009
Location: Copenhagen, Denmark
Distribution: ubuntu
Posts: 9

Original Poster
Rep: Reputation: 1
Thanks Kenhelm,

That did the trick.
 
Old 02-09-2011, 03:14 PM   #6
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,365
Blog Entries: 3

Rep: Reputation: 86
Quote:
Originally Posted by Kenhelm View Post
Use single quotes instead of double quotes.
$A in double quotes gets expanded to 'hi' before the bash -c command is executed
Code:
bash -c 'export A=blah; echo $A'
blah
(blush) I didn't notice that and should have.
Thanks for the clarification.

~~~ 0;-Dan
 
Old 02-09-2011, 03:18 PM   #7
SaintDanBert
Senior Member
 
Registered: Jan 2009
Location: Austin, TX
Distribution: Mint-15 with Cinnamon & KDE
Posts: 1,365
Blog Entries: 3

Rep: Reputation: 86
Oh! And you can do this:
Code:
prompt$ $ bash -xc "export A=junk; echo $A"

+ export A=junk
+ A=junk
+ echo stuff
stuff
compared with
Code:
prompt$ $ bash -xc 'export A=junk; echo $A'

+ export A=junk
+ A=junk
+ echo junk
junk
The -x option to bash traces each command as it runs.

Cheers,
~~~ 0;-Dan
 
1 members found this post helpful.
  


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
bash search for a pattern within a string variable nutthick Programming 7 03-06-2012 03:54 AM
[SOLVED] Bash concatenating string to variable abercrombieande Programming 4 01-19-2011 07:04 AM
[SOLVED] BASH: if Variable -eq String not working worm5252 Programming 2 01-24-2010 03:07 PM
extract string from file to variable [BASH] NikosNL Programming 13 05-07-2008 09:43 AM
Bash variable string expansion Reginald0 Linux - Software 5 02-13-2007 10:38 AM


All times are GMT -5. The time now is 11:18 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration