[SOLVED] $HOME/.bash_profile vs. $HOME/.bashrc: few questions
SlackwareThis Forum is for the discussion of Slackware Linux.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
$HOME/.bash_profile vs. $HOME/.bashrc: few questions
Hi,
I'm currently reading through a pile of books about the Bash shell and taking extensive notes. Over the years, I've learned scripting on the job, and I've decided to get a better and more "academic" grasp on it.
Right now I'm experimenting on a vanilla Slackware 14.1 installation. According to my book "Programmation Shell sous Unix/Linux", .bash_profile can be named .bash_login or .profile, and is supposed to contain definitions of variables as well as their eventual exports, as well as some system parameter definitions like umask. What .bash_profile is not supposed to contain is shell options and aliases.
So to begin some experimentation, I put a custom PATH in ~/.bash_profile:
Code:
PATH=$PATH:/sbin:$HOME/bin:$HOME/Scripts
Testing is conclusive. The PATH variable is redefined in login shells as well as in non-login shells. In runlevel 3 I can log in, and echo $PATH shows the additional paths. When I fire up WindowMaker (startx) and open an XTerm, I get the same result. Same thing for an SSH session opened from a remote machine.
But here's something weird. Next thing I add a custom PS1 to ~/.bash_profile, like this:
After reconnecting, the login shell uses the newly defined PS1 OK. Same thing for a shell opened from a remote machine. But when I launch WindowMaker and open XTerm, I get a different command prompt:
Code:
bash-4.2$ _
So how does it come that PATH gets read correctly by my various shells, but not PS1?
EDIT: sorry for the (probably unrelated) noise I wrote before: most probably you are talking about this.
so, if you want your PS1 to be used also by your shells under X you are better of defining it in ~/.bashrc (and eventually source this also in ~/.bash_profile).
I guess the best solution is to simply source .bashrc in .bash_profile:
Code:
# $HOME/.bash_profile
# $HOME/.bashrc
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
And then put everything in .bashrc:
Code:
# $HOME/.bashrc
# PATH
PATH=$PATH:/sbin
# Alias
alias ls='ls --color=auto'
alias ll='ls -al'
alias ..='cd ..'
alias ...='cd ../..'
alias cp='cp -i'
alias rm='rm -i'
alias mv='mv -i'
alias vi='vim'
alias uman='GROFF_ENCODING=utf8 man'
# PS1
GREEN='\[\033[0;32m\]'
WHITE='\[\033[1;37m\]'
NC='\[\033[0;m\]'
PS1="$GREEN[$WHITE\u$NC@$WHITE\h$NC:$WHITE\W$GREEN] \$ $NC"
# Vim
EDITOR=vim
VISUAL=$EDITOR
export EDITOR VISUAL
I'm currently reading through a pile of books about the Bash shell and taking extensive notes. Over the years, I've learned scripting on the job, and I've decided to get a better and more "academic" grasp on it.
Right now I'm experimenting on a vanilla Slackware 14.1 installation. According to my book "Programmation Shell sous Unix/Linux", .bash_profile can be named .bash_login or .profile, and is supposed to contain definitions of variables as well as their eventual exports, as well as some system parameter definitions like umask. What .bash_profile is not supposed to contain is shell options and aliases.
So to begin some experimentation, I put a custom PATH in ~/.bash_profile:
Code:
PATH=$PATH:/sbin:$HOME/bin:$HOME/Scripts
Testing is conclusive. The PATH variable is redefined in login shells as well as in non-login shells. In runlevel 3 I can log in, and echo $PATH shows the additional paths. When I fire up WindowMaker (startx) and open an XTerm, I get the same result. Same thing for an SSH session opened from a remote machine.
But here's something weird. Next thing I add a custom PS1 to ~/.bash_profile, like this:
After reconnecting, the login shell uses the newly defined PS1 OK. Same thing for a shell opened from a remote machine. But when I launch WindowMaker and open XTerm, I get a different command prompt:
Code:
bash-4.2$ _
So how does it come that PATH gets read correctly by my various shells, but not PS1?
Cheers,
Niki
If you didn't launch xterm as a login shell, you won't load anything in ~/.bash_profile
~/.bashrc should be loaded by *any* bash invocation by you; that includes any scripts that you run.
I guess the best solution is to simply source .bashrc in .bash_profile:
...
And then put everything in .bashrc:
...
I think this will double your PATH, just to mention one negative side effect
PS1 in bashrc is ok, as aliases
having both with the same content is not useful
they have different functions, unify them by sourcing one into the other disables that.
profile is for all, rc is mor dynamic and for the next, you can change it and it will work for all next sessions, profile needs log out log in
you can make use of that
btw
if a program/windowmanager starts bash with --noprofile or --norc both does not help
I think KDE konsole is good in ignoring some of the init files
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.