LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   /etc/profile VERSUS /home/.bash_profile (https://www.linuxquestions.org/questions/linux-newbie-8/etc-profile-versus-home-bash_profile-370661/)

carbono 10-07-2005 10:49 AM

/etc/profile VERSUS /home/.bash_profile
 
Hi everybody,

I use Debian and I added two environment variables to my .bash_profile file (NEW_ENV_VAR=/pathToEnvVariable; export NEW_ENV_VAR;). I also added a directory to the $PATH (PATH=%PATH:/NewDirToPATH;).

The problem is that I rebooted and when I start a terminal (bash) and issue the "env" command, my new $PATH and env. variables aren't there, although they are in my .bash_profile. Why? And what's the difference between .bash_profile and /etc/profile? Should I put my commands (mentionned above) in /etc/profile instead of .bash_profile?

Thanks a lot

Gui

morrolan 10-07-2005 10:52 AM

Try putting them in ~/.bashrc instead.

I might have this the wrong way round, but .bash_profile invokes .bashrc, (or should do) - only one is called (.bashrc I think) at the CLI, whereas .bash_profile is called in the GUI, so it still calls .bashrc.

as far as I know, etc/profile is for ALL users.

What exactly does your .bash_profile look like, or at least the lines you have added?

Please post the lines below.

carbono 10-07-2005 10:57 AM

Quote:

Please post the lines below.

# ~/.bash_profile: executed by bash(1) for login shells.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/login.defs
#umask 022

# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# the rest of this file is commented out.

# set PATH so it includes user's private bin if it exists
#if [ -d ~/bin ] ; then
# PATH=~/bin:"${PATH}"
#fi

# do the same with MANPATH
#if [ -d ~/man ]; then
# MANPATH=~/man${MANPATH:-:}
# export MANPATH
#fi

HDFLOOKMAPS=/usr/local/Maps;
export HDFLOOKMAPS;

HDFLOOKTMP=/usr/tmp;
export HDFLOOKTMP;

PATH=$PATH:/usr/bin/perl;

morrolan 10-07-2005 11:21 AM

I might be wrong in this, but you are exporting your variables to your $PATH, and then you are redefining your $PATH as purely /usr/bin/perl, overwriting all of your previous work.

Try defining your own variables AFTER the "PATH=$PATH:/usr/bin/perl;"

Or, instead of using "export", learn from a point earlier in the script which is commented out:

Code:

# set PATH so it includes user's private bin if it exists
#if [ -d ~/bin ] ; then
# PATH=~/bin:"${PATH}"
#fi

So, 3rd line deals with the path and basically adds ~/bin to your PATH (previous line checks if it exists). Instead of using export, try the following:

Code:

HDFLOOKMAPS=/usr/local/Maps;
PATH=HDFLOOKMAPS:"${PATH}";

HDFLOOKTMP=/usr/tmp;
PATH=HDFLOOKTMP:"${PATH}";

I think that's right anyway, although I haven't tried it, so PLEASE MAKE A BACKUP FIRST.

Also, why do you only have "/usr/bin/perl" as your path? Do you not use anything else but perl? Logically, you should have /bin;/sbin;/usr/bin;$HOME/bin; etc?

carbono 10-07-2005 01:22 PM

Thanks morollan, but in fact I think your wrong (or maybe I am?)

PATH=$PATH:/usr/bin/perl;

The command above doesn't override the previous $PATH. Instead, it appends "/usr/bin/perl" to end of the existing $PATH, as

PATH=/usr/bin/perl:$PATH;

would put "/usr/bin/perl" at the beginning of $PATH, followed by the other directories in already in $PATH.


I think environment variables are something different than PATH (maybe I'm wrong here, someone please correct me if I am). That's why I think that creating my 2 env. variables before adding "/usr/bin/perl" to my PATH doesn't make any difference to PATH. They're different things.

Try it for yourself: at the command line, create an env. variable (ENVVAR=/home; export ENVVAR;) and then try the "env" command. You will see your new ENVVAR and you will also see what's inside PATH (e.g. PATH=/usr/bin:/usr/local/bin:/bin)

So I'm still stuck with my problem: how can I permanently create 2 env. variables and also, how can I change my PATH permanently?

morrolan 10-10-2005 01:41 PM

Do you need to do it the way you have stated? Or is any way OK as long as it works?

I'm interested in your problem, and I've been thinking about when I should be thinking of my own problems! :D

I can add the variables to your PATH, but do you need the variables so you can call them later? so alias HDSLOOKMAPS='cd /usr/local/Maps' won't do?

I'll keep working on it.

Tinkster 10-10-2005 04:58 PM

Quote:

Originally posted by carbono
# ~/.bash_profile: executed by bash(1) for login shells.
# see /usr/share/doc/bash/examples/startup-files for examples.
# the files are located in the bash-doc package.

# the default umask is set in /etc/login.defs
#umask 022

# include .bashrc if it exists
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi

# the rest of this file is commented out.

# set PATH so it includes user's private bin if it exists
#if [ -d ~/bin ] ; then
# PATH=~/bin:"${PATH}"
#fi

# do the same with MANPATH
#if [ -d ~/man ]; then
# MANPATH=~/man${MANPATH:-:}
# export MANPATH
#fi

HDFLOOKMAPS=/usr/local/Maps;
export HDFLOOKMAPS;

HDFLOOKTMP=/usr/tmp;
export HDFLOOKTMP;

PATH=$PATH:/usr/bin/perl;

The thing is that if you're opening an xterm while logged into a
graphical environment the bash in that terminal ISN'T a login-
shell, and ONLY stuff in ~/.bashrc will be pulled.



Cheers,
Tink

init100 10-10-2005 05:42 PM

PATH=$PATH:/usr/bin/perl;

First, note that the semicolon (;) is not necessary at the end of the line above.

Then, I'm curious to why you want to add /usr/bin/perl to your PATH. Usually, PATH is a list of directories and does not include program filenames. Just adding /usr/bin to your PATH should make sure that you can access perl, as well as all the other programs in /usr/bin.

I think environment variables are something different than PATH (maybe I'm wrong here, someone please correct me if I am). That's why I think that creating my 2 env. variables before adding "/usr/bin/perl" to my PATH doesn't make any difference to PATH. They're different things.

I don't understand what you mean. PATH is just one environment variable among many, and it is only special in that your command shell (bash) interprets it as a list of directories to search for program files when you type a command without specifying its absolute path.

So I'm still stuck with my problem: how can I permanently create 2 env. variables and also, how can I change my PATH permanently?

Maybe the answer to that question is the answer to your question about the various profile and rc files. I'll try to explain the difference between those files.

There are four files of interest:
  • /etc/profile
  • /etc/bashrc
  • ~/.bash_profile
  • ~/.bashrc
The first two files are system-wide configuration files for bash. Anything that you put in them will affect all users on the system. The last two are your user configuration files for bash. Any modifications to these files will only affect your user.

The files /etc/profile and ~/.bash_profile are used when bash is started as a login shell. This usually happens when you log into your X Windows session or log in remotely, using e.g. SSH. Otherwise, the /etc/bashrc and ~/.bashrc files are used. In both cases, the system-wide file is read first, and the user-specific file second.

Setting the PATH variable is usually done in the profile files, since you only want to set those one, not every time you open a new X terminal.

I hope this helps!

morrolan 10-11-2005 08:10 AM

I'm kinda getting the drift now - I've been experimenting and had partial success.

I take your point about /usr/bin/perl not being your PATH and actually being appended to it, I didn't read it properly the first time. :p

in .bashrc it defines your PATH - you can simply add :/usr/local/Maps:/usr/tmp to the end of that, or you can append it just like you said:

Code:

PATH=$PATH:/usr/local/Maps:/usr/tmp
At the end of .bash_profile. You can also define variables, i.e.

Code:

HDSLOOKMAPS=/usr/local/Maps
I think the problem is, you are trying to export the variable and not the variables target into the PATH, which doesn't appear to work. Also, with the ENV variables, I've only had partial success using them, as below:

Code:

cd HDSLOOKMAPS
I get a message stating that it is a directory, but if I do:

cd $HDSLOOKMAPS
it doesn't produce an error, but it leaves me in the current directory and doesn't move me. I assume ENV variables still use the $ sign, as $HOME and $PATH do.

You could also try aliasing instead of variables.

Code:

alias HDSLOOKMAPS='/usr/local/Maps'
but it all depends on the intended use of the variable, which you haven't made clear. :confused:

carbono 10-11-2005 09:10 AM

Thanks all for clarifying the use of environment variables and the differences between the 4 bash configuration files.

1-I'd like to come back to the use of /usr/bin/perl in my PATH. I realize now that this doesn't make much sense since /usr/bin is already in my PATH and perl is a file located inside /usr/bin. I was getting desperate with another problem with perl so I thought that adding /usr/bin/perl to PATH would help. I didn't think much, this is my mistake.

2- About the use of aliases, well, I don't know a thing about'em. Maybe this would be the solution. I need to set these 2 environment variables (HDFLOOKMAPS and HDFLOOKTMP) to use a software that needs them. If they don't exist, the software pops a window telling me that I need to create these variables.

The reason why these variables are not created every time I log in, I think, is because when I turned on my computer, I log in via some kind of Debian login manager into the Gnome Desktop Environment and maybe this is why the content of ~/.bash_profile isn't executed when, inside Gnome, I open a terminal???

Could this be the problem? And what is the solution to it?

Thanks again all.

morrolan 10-11-2005 09:41 AM

If you create the variables in .bashrc, they will be created whatever you do, because .bash_profile calls .bashrc anyway.

You want to do:

Code:

alias HDFLOOKMAPS='/usr/local/Maps'
alias HDFLOOKTMP='/usr/tmp'

Does the program need to run as root? If so, it won't use your users aliases or variables, althought I think there is a way to preserve them with su or sudo. (can someone clear this up for me please?)

Once saved, press CTRL + ALT +F2 and login. Once logged in, type:

Code:

user@linuxbox ~$ echo $HDFLOOKMAPS
/usr/local/Maps
user@linuxbox ~$

That will check that the variable exists.

Tinkster 10-11-2005 12:23 PM

aliases aren't environment variables, aliases allow you to
create synonyms to command sequences without having
to put them in bash-script. What you really wanted is an
Code:

export HDFLOOKMAPS='/usr/local/Maps'
export HDFLOOKTMP='/usr/tmp'

or, in case all you'll ever do with those values
is to cd into the directories
Code:

alias HDFLOOKMAPS='cd /usr/local/Maps'
alias HDFLOOKTMP='cd /usr/tmp'


Cheers,
Tink

carbono 10-12-2005 09:23 AM

Thanks a lot Folks!

I added

Code:

export HDFLOOKMAPS='/usr/local/Maps'
Code:

export HDFLOOKTMP='/usr/tmp'
to .bashrc and I verified that they were set with

Code:

echo $HDFLOOKMAPS
Code:

echo $HDFLOOKTMP
Thanks agin!

Gui


All times are GMT -5. The time now is 01:19 AM.