exporting variable


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting exporting variable
# 1  
Old 07-02-2010
exporting variable

Hi All;

I m working on a script and came across an issue ,to explain it briefly here is the sample code

Code:
echo $HOSTNAME|while read IN; do var=`echo $IN|awk -F "-" '{print $2}'`; export var; echo $var; done

now I get the value of $var but when it is out of the while loop
it does not return me a value.

I tried using typeset,still the same issue

So the question is when i set a value for a variable and export it in a while loop the value of the variable is not returned after I echo it out of the loop.

Thanks in advance
Syed

P.S : It can be done a one-liner; but my requirement it to run it through an array so I need the while loop.

Last edited by pludi; 07-02-2010 at 02:05 PM.. Reason: code tags, please...
# 2  
Old 07-02-2010
Please state the Operating System and which Shell. As this is behaving like an old Bourne shell, do you have a Posix shell or ksh available?
# 3  
Old 07-02-2010
Lightbulb Updated details

Sorry ,I initially forgot to add details.Im using RHEL 5 and shell is bash

---------- Post updated at 01:16 AM ---------- Previous update was at 01:13 AM ----------

Finally it works,I have fixed it

Code:
for IN in $HOSTNAME; do var=`echo $IN|awk -F "-" '{print  $2}'`;  export var; echo $var; done


Last edited by Scott; 07-02-2010 at 05:28 PM.. Reason: Code tags, please...
# 4  
Old 07-03-2010
An "export" statement only works in the current shell or subshell and makes a variable available to any scripts you call. It does not make the variable available to the parent shell. In this case the "export" statement is surplus in both examples. I can't explain in this context why "while" created a subshell when "for" did not.
cfajohnson may know better.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problems setting or exporting variable when using multiple commands from same line.

I am experimenting with some scripting as a way to learn more about it. I have a simple script that calls two other scripts. Each script echos some stuff to prove it ran and then sets a simple variable and exports it. I cannot get one of the variables to display back in the main calling script... (2 Replies)
Discussion started by: scottrif
2 Replies

2. Shell Programming and Scripting

Export command variable exporting problem

I have a txt file from which i am assiging a value to a variable using the code in script1 script1.sh export f=$(sed -n "/Freq *=/ s/.*= *//p" ${R_path}/output.txt) echo "$f" --------> this works in script2 ( which executes the script1) eval ./script1.sh if && ; then echo... (1 Reply)
Discussion started by: shashi792
1 Replies

3. Shell Programming and Scripting

Exporting my dynamical variable won't work?

Even though the idea "might" not be great I still wrote this piece of code to get practice.. Which means that it is the CODE that matters here. Anyways; The intension is to create a program(or do we call it script?) that searches recursively through a folder to find a file - stored in a... (4 Replies)
Discussion started by: Pesk
4 Replies

4. Solaris

variable exporting

Hi, can anyone tell me the difference between the below two examples: Eg-1: # name=bravo # echo $bravo what would be the o/p Eg-2: # name1=jhonny # export name1 # echo $name1 what would be the o/p If the o/p's of both examples are the same then what is the use of the cmd export... (3 Replies)
Discussion started by: rahul_11d
3 Replies

5. Shell Programming and Scripting

Exporting to excel

Can someone help me out to export awk output to excel file in different rows? Plzzzz its urgent...:( (4 Replies)
Discussion started by: goutam_igate
4 Replies

6. Shell Programming and Scripting

Exporting Value of a Variable as a Variable

Here's how; Say; I have a variable VAR which contains something like MY_DIR=/apphome/some/mydir. What I want is I want to export VAR so that it will vitually export MY_DIR. Please suggest me? (15 Replies)
Discussion started by: swmk
15 Replies

7. Shell Programming and Scripting

Exporting variable from config file

Hi, I am exporting the environment variable from config file, but when I echo the variable it does not display any value. Here is the snippet of the code #!/bin/sh export ENVIRONMENT_ROOT_DIRECTORY="/cb/$ENVIRONMENT" echo $ENVIRONMENT_ROOT_DIRECTORY ${JAVA_HOME}/bin/java... (2 Replies)
Discussion started by: bhavnabakshi
2 Replies

8. Windows & DOS: Issues & Discussions

Exporting data

My friend's boss has asked me to help update a customer database. They have a limited amount of space and have asked if I can do this from my home computer. She is working on a Unix system with a program called Accuterm. My understanding is that this program is a dedicated program for the... (0 Replies)
Discussion started by: clearchoice
0 Replies

9. UNIX for Dummies Questions & Answers

exporting display

how do I export my display from my unix box to my pc basically what is the syntax for export DISPLAY (2 Replies)
Discussion started by: csaunders
2 Replies

10. UNIX for Advanced & Expert Users

exporting a directory

How do I export a directory ? I am trying to link the directory from another machine. (2 Replies)
Discussion started by: brv
2 Replies
Login or Register to Ask a Question