BASH script to export var to env


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH script to export var to env
# 1  
Old 11-14-2014
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

Code:
#!/bin/bash

read -s -p "Enter Password: " gfpassword

TMP=`tempfile`

echo "AS_ADMIN_PASSWORD=$gfpassword" > $TMP

GF=$TMP

export GF

Any help would be fantastic and thank you

Last edited by Corona688; 11-14-2014 at 02:48 PM..
# 2  
Old 11-14-2014
Code:
TMP=`tempfile`

The back ticks are to execute a program name tempfile and assign its output to a variable name TMP.

This would be what you want.
Code:
TMP="/path/to/tempfile"

# 3  
Old 11-14-2014
Thank you.

I made the following change and if I do a echo on $GF it definitely shows me the path to the temp file created however it still isn't add $GF to the environment variables Smilie

Code:
#!/bin/bash

read -s -p "Enter Password: " gfpassword

TMP="$(tempfile)"

echo "AS_ADMIN_PASSWORD=$gfpassword" > $TMP

GF=$TMP

echo $GF

export GF

Code:
glass@xxxxxxx:~/scripts$ ./rbtest.sh 
Enter Password: /tmp/fileZo2wDX


Last edited by Corona688; 11-14-2014 at 02:49 PM..
# 4  
Old 11-14-2014
Edited: Not clear of what its wanted. Sorry.

Last edited by Aia; 11-14-2014 at 03:00 PM..
# 5  
Old 11-14-2014
No Aia, that is wrong.

koikoi, remember that environment variables are inherited from parent processes to child processes. The environment is never actually shared, just copied. Children receive copies when they are created.

When you run a script with ./script or /path/to/script you are creating a new child process to run it in. The variable gets exported just fine in this new child. It then finishes and quits, returning to your parent process which of course remains unchanged.

To export it in your own shell, you must run it in your own shell, called "sourcing" it. The operator for source is "dot space filename", i.e. . ./script.sh
This User Gave Thanks to Corona688 For This Post:
# 6  
Old 11-14-2014
Thank you so much ... I have been fighting this for a few hours.
This User Gave Thanks to koikoi For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Invoking a bash shell with an export var

Hello all, How can I invoke the bash shell (via command line) to execute another command by setting an exported environmental variable on the fly (as this env var would be used by the command -another script, the bash would execute). This needs to be done in one single line as the same would... (4 Replies)
Discussion started by: Praveen_218
4 Replies

2. Shell Programming and Scripting

Bash script - ENV Variable context problem using su

Hello I have found some piece of code to verify and then run shell script with root permission from normal user. see : http://blog.mecworks.com/articles/2006/02/23/bash-scripting-tip-running-a-script-as-root I have wrote two scripts using this tips. - one to copy file from server to local... (6 Replies)
Discussion started by: jcdole
6 Replies

3. Shell Programming and Scripting

how to modify existing env var

greetings, i have the following env variable: LSB_HOSTS='t70cra102 t70cra102 t70cra108 t70cra108'the variable could be any number of host names and sometimes the same name several times. i was hoping there's a way to turn it into the following: NEW_LSB_HOSTS=,,,]always appreciate the help... (2 Replies)
Discussion started by: crimso
2 Replies

4. UNIX for Advanced & Expert Users

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... (8 Replies)
Discussion started by: idro
8 Replies

5. Solaris

export vs env vs set commands

Hi I'm trying to understand variable scopes in solaris10. It is said that to display env variables we use 3 commands : - env - set - export What is the difference between them ? thx for help. ---------- Post updated at 11:00 AM ---------- Previous update was at 10:50 AM ---------- ... (2 Replies)
Discussion started by: presul
2 Replies

6. Shell Programming and Scripting

export and access env variable in two scripts

Hi, I have two scripts say one.sh and two.sh. I want one.sh to continuously export a variable in loop. and when two.sh starts then it should read the last value exported from one.sh. file: one.sh #! bin/sh for i in `seq 1 1 4000000`; do export VAR=$(($i**$i)) ; done file two.sh ... (2 Replies)
Discussion started by: bhushan123
2 Replies

7. Shell Programming and Scripting

Set/Export Env Vars from with Shell Script With Input Variable

I have a shell script I want to run that will set environment variables based on the value of an input variable submitted when the shell script is called. For example: $ mgenv.sh prod This would set environment variables for prod $ mgenv.sh test This would set environment variables... (1 Reply)
Discussion started by: brtaylor73
1 Replies

8. UNIX for Advanced & Expert Users

How to export ENV variables, which remains set for all the shell

Hi ! How to export ENV variables, which remains set for all the shell Example :- Login :myID Pwd : **** -> Here my ID .profile is executed. Let say I set MYENV variable Kisses% rlogin ABC -l XXXGroupID -> I login into a remote Solaris Server ABC password : **** -> "XXXGroupID's... (1 Reply)
Discussion started by: dashok.83
1 Replies

9. Shell Programming and Scripting

export env variables

hi i want to write a shell script to set environment variables . But i am not been able to set that for the current shell instead i have to spawn a new shell. Is there a way to set the env variable for the current shell using shell script in bash shell ? Thnx (2 Replies)
Discussion started by: varun.81
2 Replies

10. Shell Programming and Scripting

Reason for EXPORT in .profile,env,etc

Hi all, I have seen it in all the .profile files and env file this PATH=/usr/bin:/etc:/usr/sbin:/usr/ucb:$HOME/bin:/usr/bin/X11:/sbin:. export PATH What exactly does this Export path does?? Thanks SUmeet (1 Reply)
Discussion started by: sumeet
1 Replies
Login or Register to Ask a Question