LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-02-2010, 06:35 AM   #1
varunisac
LQ Newbie
 
Registered: Dec 2010
Posts: 1

Rep: Reputation: 0
Shell Scripting doubt


Hi,

I had an environment Variable ABC set from the command line export ABC=hello

I want to use this in the following way

VER=2000 ABC=$ABC/script/$VER myScript myProgram

After the execution of this line the env wants to remain unchanged. that is
env|grep 'ABC' must show ABC=hello.

Helps will be appreciated !!!
 
Old 12-02-2010, 06:53 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Experiment shows no can do; if you assign a value to a variable that is an environment variable then the environment variable is changed. Maybe you are asking us to fix a sub-optimal solution; what do you want to achieve?
 
Old 12-02-2010, 07:35 AM   #3
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 447Reputation: 447Reputation: 447Reputation: 447Reputation: 447
Hi

You can use () so the shell will do a fork. But the variable changing can't be on the same line because the shell will resolve variables before they are changed.


Code:
#!/bin/sh
ABC="hello"
(
VER=2000
ABC=$ABC/script/$VER
echo "ABC=$ABC"
)
echo "ABC=$ABC"
 
Old 12-02-2010, 11:27 AM   #4
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Experiment shows no can do; if you assign a value to a variable that is an environment variable then the environment variable is changed
Um
Code:
~$ export ABC=hello
~$ VER=2000 ABC=$ABC/script/$VER sh -c 'echo $VER, $ABC'
2000, hello/script/2000
~$ env | grep ABC
ABC=hello
 
Old 12-02-2010, 11:46 AM   #5
otheus
LQ Newbie
 
Registered: Jun 2006
Location: Austria
Distribution: RHEL AS 4
Posts: 26

Rep: Reputation: 16
Quote:
Originally Posted by ntubski View Post
Um
Code:
~$ export ABC=hello
~$ VER=2000 ABC=$ABC/script/$VER sh -c 'echo $VER, $ABC'
2000, hello/script/2000
~$ env | grep ABC
ABC=hello
When you told the shell to run a program with a specific environment variable set to whatever, it did exactly that for the new process created. It did not change the environment variable in the current environment.

This is so something like this will work and not screw things up:
Code:
$ export LD_LIBRARY_PATH="/usr/X/lib:/usr/lib"
$ LD_LIBRARY_PATH="/usr/X/compatlib:/usr/compatlib" oldprogram
$ normalprogram
The kernel sees the "compat" version of LD_LIBRARY_PATH when it tries to run "oldprogram", but the default version when it runs "normalprogram".
 
Old 12-02-2010, 11:54 AM   #6
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,781

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by otheus View Post
When you told the shell to run a program with a specific environment variable set to whatever, it did exactly that for the new process created. It did not change the environment variable in the current environment.
Right, it seemed to me that the OP was in fact asking to not change the environment variable in the current environment even though this is the default behaviour. Possibly I've totally misunderstood what everyone has been saying, if so just ignore me.


Last edited by ntubski; 12-02-2010 at 11:55 AM. Reason: grammar
 
  


Reply



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 scripting doubt bala1486 Linux - Newbie 4 04-09-2010 05:49 AM
Doubt in awk scripting.... stalin2020 Programming 1 06-02-2008 07:20 AM
Doubt in shell script malru AIX 2 11-24-2006 07:59 AM
teaching shell scripting: cool scripting examples? fax8 Linux - General 1 04-20-2006 04:29 AM
Scripting doubt heraklez Slackware 2 02-05-2006 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 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