LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware
User Name
Password
Slackware This Forum is for the discussion of Slackware Linux.

Notices


Reply
  Search this Thread
Old 07-21-2009, 05:08 AM   #1
TL_CLD
Member
 
Registered: Sep 2006
Posts: 366

Rep: Reputation: 45
Enviroment variables in .bash_profile


Hey all,

I have a weird problem. In my /~/.bash_profile file I have this:

Code:
ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/usr/local/lib/gnat
export ADA_PROJECT_PATH
I boot to runlevel 3. When I log in, and echo $ADA_PROJECT_PATH, I get this:

Code:
:/usr/local/lib/gnat
Which is exactly what I should get.

But if I then run startx (I use KDE), and start a Konsole and echo $ADA_PROJECT_PATH again, I get this:

Code:
:/usr/local/lib/gnat:/usr/local/lib/gnat
What the?? Why are there now two paths??

Booting to runlevel 4 changes nothing. As soon as KDE is up and running, I have multiple paths, instead of just the one I'm actually exporting in .bash_profile.

Hopefully this is just some minor thing I'm not doing right, but it's completely eluding me.

I hope someone can help.

/Thomas
 
Old 07-21-2009, 05:22 AM   #2
dahongpalay
LQ Newbie
 
Registered: Aug 2008
Location: Philippines
Distribution: LFS, Slackware, Slamd64
Posts: 6

Rep: Reputation: 1
i think it should be;

Code:
ADA_PROJECT_PATH=/usr/local/lib/gnat
export ADA_PROJECT_PATH
 
Old 07-21-2009, 05:47 AM   #3
TL_CLD
Member
 
Registered: Sep 2006
Posts: 366

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by dahongpalay View Post
i think it should be;

Code:
ADA_PROJECT_PATH=/usr/local/lib/gnat
export ADA_PROJECT_PATH
Yes, but how can I then preserve additional paths added to the variable?

Code:
ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/usr/local/lib/gnat
ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/some/other/path
ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/and/a/third/path
Without the $ADA_PROJECT_PATH part, that will fail, and only the last path is added to ADA_PROJECT_PATH.
 
Old 07-21-2009, 06:05 AM   #4
TL_CLD
Member
 
Registered: Sep 2006
Posts: 366

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by dahongpalay View Post
i think it should be;

Code:
ADA_PROJECT_PATH=/usr/local/lib/gnat
export ADA_PROJECT_PATH
I've just confirmed this: If I dump the $ADA_PROJECT_PATH: part, then the paths set in /etc/profile are deleted/written over.

It is a mystery to me why my bash_profile files are being sourced more than once, and why it appears only to happen when i start X.
 
Old 07-21-2009, 08:58 AM   #5
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,493
Blog Entries: 32

Rep: Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665
Hi,

Quote:
excerpt from 'man bash';

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
Try placing your path in '.bashrc'.


You could setup a .bashrc & .bash_profile for your user;

Code:
sample .bash_profile;

~$ cat .bash_profile
# .bash_profile
#08-30-06 12:21 
#
# Source .bashrc
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi
Code:
sample .bashrc;
:~$ cat .bashrc

#.bashrc
#08-30-06 12:20 

# Add bin to path

export PATH="$PATH:/sbin:/usr/sbin:$HOME/bin"

#export PATH="$PATH:$HOME/bin"

# Dynamic resizing
shopt -s checkwinsize

# Custom prompt
#PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

#08-29-06 11:40 gws

if [ `id -un` = root ]; then
   PS1='\[\033[1;31m\]\h:\w\$\[\033[0m\] '
 else
   PS1='\[\033[1;32m\]\h:\w\$\[\033[0m\] '
fi

#
# Add color
eval `dircolors -b`

# User defined aliases
alias cls='clear'
alias clls='clear; ls'
alias ll='ls -l'
alias lsa='ls -A'
alias lsg='ls | grep'
alias lsp='ls -1 /var/log/packages/ > package-list'
alias na='nano'
alias web='links -g -download-dir ~/ www.google.com'

#08-29-06 11:50

#To clean up and cover your tracks once you log off
#Depending on your version of BASH, you might have to use
# the other form of this command
   trap "rm -f ~$LOGNAME/.bash_history" 0

#The older KSH-style form
#   trap 0 rm -f ~$LOGNAME/.bash_history
 
Old 07-21-2009, 02:10 PM   #6
grissiom
Member
 
Registered: Apr 2008
Location: China, Beijing
Distribution: Slackware
Posts: 423

Rep: Reputation: 45
I just wonder whether Konsole is a login shell or not. I remember I have had such problem in KDE3...
 
Old 07-21-2009, 02:13 PM   #7
jmacloue
LQ Newbie
 
Registered: Apr 2009
Posts: 13

Rep: Reputation: 1
Well, .bash_profile is executed after you log in to the system - that's when you enter your username and password at the console _and_ when you start your shell in Konsole as a login shell (seems it is so by default - can't check though, I don't use KDE).

So, when you initially log in to the system, .bash_profile sets ADA_PROJECT_PATH to ":/usr/local/lib/gnat". Then you fire up KDE and Konsole executes bash again - but in this case ADA_PROJECT_PATH is already in your environment so it gets expanded and the value is "duplicated".

On the other hand, .bashrc is executed every time the bash is run so moving your ADA_PROJ_PATH routine there will duplicate the variable as well. If it really creates any problems you can try smth like that:

Code:
[ -z "$ADA_PROJECT_PATH_SET"] && export ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/usr/local/lib/gnat
export ADA_PROJECT_PATH_SET=1
in your .bash_profile. This will set ADA_PROJECT_PATH_SET variable upon initial login and then bash invoked from Konsole will skip the assigment.

But better way will be to make Konsole invoke bash in regular, not "login shell" mode.
 
Old 07-21-2009, 03:21 PM   #8
onebuck
Moderator
 
Registered: Jan 2005
Location: Summer Midwest USA, Central Illinois, Winter Central Florida
Distribution: SlackwareŽ
Posts: 13,493
Blog Entries: 32

Rep: Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665Reputation: 2665
Hi,

Sorry but the '/etc/profile' will be used if it exists so when you login initially when bash is invoked then the use of ~/.bash_profile, ~/.bash_login, and ~/.profile is read until something that is read and executable then that is executed;

Quote:
excerpt from 'man bash';

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When a login shell exits, bash reads and executes commands from the file ~/.bash_logout, if it exists.

When an interactive shell that is not a login shell is started, bash reads and executes commands from ~/.bashrc, if that file exists. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of ~/.bashrc.
A user should setup the '.bashrc' whenever the interactive is not a login shell.

That's one of the advantages of using a '~/.bash_profile' that contains conditional tests to execute the '~/.bashrc'.
 
Old 07-22-2009, 02:00 AM   #9
TL_CLD
Member
 
Registered: Sep 2006
Posts: 366

Original Poster
Rep: Reputation: 45
Quote:
Originally Posted by jmacloue View Post
Well, .bash_profile is executed after you log in to the system - that's when you enter your username and password at the console _and_ when you start your shell in Konsole as a login shell (seems it is so by default - can't check though, I don't use KDE).

So, when you initially log in to the system, .bash_profile sets ADA_PROJECT_PATH to ":/usr/local/lib/gnat". Then you fire up KDE and Konsole executes bash again - but in this case ADA_PROJECT_PATH is already in your environment so it gets expanded and the value is "duplicated".

On the other hand, .bashrc is executed every time the bash is run so moving your ADA_PROJ_PATH routine there will duplicate the variable as well. If it really creates any problems you can try smth like that:

Code:
[ -z "$ADA_PROJECT_PATH_SET"] && export ADA_PROJECT_PATH=$ADA_PROJECT_PATH:/usr/local/lib/gnat
export ADA_PROJECT_PATH_SET=1
in your .bash_profile. This will set ADA_PROJECT_PATH_SET variable upon initial login and then bash invoked from Konsole will skip the assigment.
This looks like a very neat and tidy way of fixing a somewhat annoying issue. Thank you.


Quote:
Originally Posted by jmacloue View Post
But better way will be to make Konsole invoke bash in regular, not "login shell" mode.
AFAIK that is standard behavior for Konsole. I think you have to explicitly tell it to start as a login shell (the --ls option), if that's what you want.

So the only other thing I can come up with, is that it happens when X and/or KDE is started.

/Thomas
 
  


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
How can I see all the enviroment variables? zvonSully Linux - Software 3 02-09-2005 08:39 AM
Enviroment variables johnnydangerous Fedora 3 02-07-2005 06:45 AM
enviroment variables cphase Linux From Scratch 7 01-19-2005 04:15 PM
Where to set enviroment variables vavoem Linux - Software 7 09-07-2004 02:13 AM
setting enviroment variables unimaginative Linux - General 5 01-08-2004 02:22 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Slackware

All times are GMT -5. The time now is 07:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration