Bash script with export variables


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Bash script with export variables
# 1  
Old 07-01-2011
Bash script with export variables

Hi all guys,

how you can read in thread title, I'm deploying a bash script in which I have to export some variables inside it.
But (I think you know) the export command works only inside the script and so, on exit command, the variables aren't set like I set inside the script.
Consequently in env file, variables are set like before the run of the script.

So, the question is easy: how can I set permanently the variables inside the script with export command?

Thx, bye.
Fabio
# 2  
Old 07-01-2011
Source the script in the current shell:

Code:
. ./script_name

Note the dot `.` shell builtin at the beginning of the line.
# 3  
Old 07-01-2011
Ok, but I need to do a similar thing inside the script.
# 4  
Old 07-01-2011
So do it.
# 5  
Old 07-01-2011
I'm sorry, but you intend for example, if my script is Test.sh e inside the code is:

Code:
#!/bin/bash
echo $1 > /tmp/output
bin_dir=$1
export bin_dir
exit 0

you mean I have to do:

Code:
#!/bin/bash
echo $1 > /tmp/output
bin_dir=$1
. ./Test.sh
export bin_dir
exit 0

right or not?

Last edited by radoulov; 07-01-2011 at 07:46 AM.. Reason: Code tags.
# 6  
Old 07-01-2011
No.
if you have a script that sets and/or exports variables that are needed by another script,
you need to source the script that sets the environment inside the script that needs the new environment.

For example:

env_script.sh

Code:
export var1=value1 var2=value2 ...

main_script.sh

Code:
# source the env script
. /path/to/env_script.sh
# here the values of var1 and var2 are available

If you need to do something different, please be more specific.
# 7  
Old 07-01-2011
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Connecting and changing variables in Bash script

#!/bin/bash X=$(</home/cogiz/computerhand.txt) # (3S 8C 2H 6D QC 8S 4H 5H) Y=$(</home/cogiz/topcardinplay.txt) # KS A=( "${Y::1}" ) B=( "${Y:1}" ) for e in ${X}; do if ]; then # searching for valid cards K,S or 8 ... (0 Replies)
Discussion started by: cogiz
0 Replies

2. Shell Programming and Scripting

BASH script to export var to env

Hi all I am trying to create a script that takes a password input then writes that to a tmp file and puts that tmp file path in my env as a var. It does everything but export the my env and I am unsure why. I am using Ubuntu 12.4 #!/bin/bash read -s -p "Enter Password: " gfpassword... (5 Replies)
Discussion started by: koikoi
5 Replies

3. Shell Programming and Scripting

Problem with variables and bash script

From the command line: dions-air:scripts dion$ ls -l /Users/dion/Library/Application\ Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCX -rw-r--r-- 1 dion staff 157934 7 Jun 06:55 /Users/dion/Library/Application Support/Garmin/Devices/3816821036/History/2014-06-07-055251.TCXworks... (2 Replies)
Discussion started by: dionbl
2 Replies

4. Shell Programming and Scripting

'Dynamic' setting of variables in bash script

Hi all, I want to dynamically set variables in a bash script. I made a naive attempt in a while loop that hopefully can clarify the idea. n=0; echo "$lst" | while read p; do n=$(($n+1)); p"$n"="$p"; done The error message is: bash: p1=line1: command not found bash: p2=line2: command... (8 Replies)
Discussion started by: jeppe83
8 Replies

5. Shell Programming and Scripting

Multiple Variables for BASH script

Hello, I am new to the whole "scripting" thing. Below is the script that I have so far and where i need the Variables to go (VAR#) #!/bin/bash #Sample Script VAR1= echo "Choose an option: 1) Create a file. 2) Delete a file. 3) Move a file." read VAR1 case $VAR1 in 1) echo "Pick... (4 Replies)
Discussion started by: eclerget
4 Replies

6. Shell Programming and Scripting

problem using variables in bash script

I am using variable to give the location of the file I am using but I get error. Here is the code: LogFile=/tmp/log.email echo -e "could not close the service - error number $error \n" > $LogFile well this is not all the code but is enough because the problem start when I try to use the... (3 Replies)
Discussion started by: programAngel
3 Replies

7. Solaris

not able to export the Variables

I am working with Sun Solaris 9 and I want to export the environment variable from my application(xxxx.ksh) but I am not able to see it when I am using SET command I am writing some variables which I have to set COMMON_USER_HOME=${HOME} export COMMON_USER_HOME echo... (6 Replies)
Discussion started by: smartgupta
6 Replies

8. Shell Programming and Scripting

script to export variables

Hi, I am a newbie to unix as well as scripting. I need to write a script, which on execution sets the necessay oracle variables. Can someone help me out as to how to proceed? also can u suggest good tutorial for bash/shell scripting? thanks (1 Reply)
Discussion started by: aboxilica
1 Replies

9. Linux

Cannot running export database using bash script

Hi all, I'm new in linux. When I try to run a bash script, it doesn't execute and i receive the following error message 20070321:220002|ERROR||exportDatabase.bash|Another EXPORT process (pid=2799) is still running. If i kill this pid, i receive "No such process". This process was running... (5 Replies)
Discussion started by: tovohery
5 Replies

10. Shell Programming and Scripting

export variables

I have a master shell, which calls another shell to export some env variables. But when I just run the child shell from the command line, and see if the variables are exported by doing, echo $EXPORTED_VAR1 I am not seeing the value. But I am sure, I am using the child shell from a master... (4 Replies)
Discussion started by: srishan
4 Replies
Login or Register to Ask a Question