LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Suse/Novell
User Name
Password
Suse/Novell This Forum is for the discussion of Suse Linux.

Notices



Reply
 
Search this Thread
Old 12-28-2005, 12:10 AM   #1
mbjunior99
Member
 
Registered: Feb 2004
Posts: 34

Rep: Reputation: 15
Bash script environment variables


Hi,

I'm currently writing a bash script running under suse 9.3 pro part of which is supposed to set some environment variables which are going to be used later on. I am just using export to set the environment variable but it doesn't seem to be sticking. As soon as the bash script has executed the variable is no longer there when I look at the list. Is there anyway to make these stick?

Thanks
 
Old 12-28-2005, 12:13 AM   #2
equinox
Member
 
Registered: Dec 2003
Location: Johannesburg, South Africa
Posts: 846

Rep: Reputation: 30
In your script just add your variables i.e

#!/bin/bash

x=2
i=4
 
Old 12-28-2005, 12:19 AM   #3
mbjunior99
Member
 
Registered: Feb 2004
Posts: 34

Original Poster
Rep: Reputation: 15
What happens if I want to reference them in another script after it has run? Would this still work?
 
Old 12-28-2005, 01:11 AM   #4
equinox
Member
 
Registered: Dec 2003
Location: Johannesburg, South Africa
Posts: 846

Rep: Reputation: 30
Nope it won't.
 
Old 12-28-2005, 01:40 AM   #5
acummings
Member
 
Registered: Jul 2004
Distribution: Slackware
Posts: 615

Rep: Reputation: 49
>What happens if I want to reference them in another script after

A concept of sub shell(s) exists here. IOW perhaps think parent--child. IOW any shell in which you set and export vars is at first the parent until your script has been launched -- script, however, (when launch it) launches in a sub shell (another shell, child to where you started at).

So, set and exported vars are available at any time (key *from said child shell*. Also key: once your script ends/quits, its child shell it ran in also closes out or terminates -- and now if you launch another script, the vars from the first script are gone, no longer able to reference.

So, either put two scripts into one script (all runs in the SAME child shell ie no referencing problem).

Or, if you need vars avail in your evironment (globally) then thats what .bash_profile and .bashrc user config files are for.

I've a way that works. Not sure if I got it the best way thats available. I modify var in .bashrc and in .bash_profile I source the .bashrc -- thus:

# begin .bash_profile:
# 3-10-2005 user al bash settings

# System wide aliases and functions /etc/bashrc (/etc/profile.d).
# Personal startup programs should go into ~/.bash_profile.
# Personal aliases and functions and environment
# variables should go into ~/.bashrc

[ -f /etc/profile ] && . /etc/profile

[ -f ~/.bashrc ] && . ~/.bashrc
# end of .bash_profile

only 2 lines there in my .bash_profile -- each line sources the config from yet another file -- each of those 2 lines says: if (find/exist a/said file) on left then on right the dot means source the file on the right. In that way, both of those two files get sourced into my ~/.bash_profile


Here's my ~/.bashrc:

# 3-10-2005 user al bash settings

# System wide aliases and functions /etc/bashrc (/etc/profile.d).
# Personal startup programs should go into ~/.bash_profile.
# Personal aliases and functions and environment
# variables should go into ~/.bashrc

[ -f /etc/profile ] && . /etc/profile
export PATH=$PATH:/home/al/bin
biff n
alias cdr='sudo /home/al/bin/cdrecordeasy'
alias cmx='chmod u+x'
alias cups='sudo /usr/local/bin/cups.sh'
alias diskck='du -s -k -c * | sort -rn'
alias firew='sudo /usr/local/bin/firehol'
alias hi='history'
alias hig='history | grep'
alias httpd='sudo /usr/sbin/apachectl'
alias fm='fetchmail -k'
alias fmorig='fetchmail -k -l 50275'
alias fmck='tail /var/tmp/fetchmail.log'
alias lsag='ls -la | grep'
alias lsg='ls | grep'
alias newsdl='sudo slrnpull -h news.server.com'
alias newsr='slrn --spool -i ~/.slrnrc_spool -f ~/.jnewsrc_pull'
alias prnsrv='sudo /sbin/arp -s -v 192.168.0.102 00C002505653'
alias prnsrvck='ping -c 4 192.168.0.102'
alias mntcd='mount /mnt/cdrom'
alias umntcd='umount /mnt/cdrom'
alias mntflp='mount /mnt/floppy'
alias umntflp='umount /mnt/floppy'
# end of .bashrc

my modified (appended to) PATH variable is available to me (globally) in any shell that I (I'm a user: al) open (or log into).

This is on Slackware 10.2

Similarities exist amongst distros, but filenames and locations may somewhat differ.

Alan.
 
  


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
Need help setting environment variables via shell script srosburg Linux - Newbie 2 12-08-2005 08:58 PM
Decimal numbers in bash script variables? Massif Programming 3 11-07-2005 10:01 PM
bash script variables twantrd Programming 7 11-17-2004 03:38 AM
Setting environment variables from shell script theta Linux - General 5 09-02-2004 09:50 PM
Setting environment variables from a script... sylvain_gnu Linux - Newbie 5 04-20-2004 01:31 PM


All times are GMT -5. The time now is 12:54 AM.

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