LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 10-19-2015, 09:10 AM   #1
kikinovak
MLED Founder
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: CentOS, OpenSUSE
Posts: 3,453

Rep: Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154
$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:

Code:
# Command prompt for non-root users
GREEN='\[\033[0;32m\]'
WHITE='\[\033[1;37m\]'
NC='\[\033[0;m\]'
PS1="$GREEN[$WHITE\u$NC@$WHITE\h$NC:$WHITE\W$GREEN] \$ $NC"
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
 
Old 10-19-2015, 09:20 AM   #2
ponce
LQ Guru
 
Registered: Aug 2004
Location: Pisa, Italy
Distribution: Slackware
Posts: 7,131

Rep: Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202Reputation: 4202
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).

Last edited by ponce; 10-19-2015 at 10:08 AM.
 
1 members found this post helpful.
Old 10-19-2015, 11:32 AM   #3
kikinovak
MLED Founder
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: CentOS, OpenSUSE
Posts: 3,453

Original Poster
Rep: Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154
Thanks, ponce.

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
Cheers,

Niki
 
Old 10-19-2015, 08:08 PM   #4
Richard Cranium
Senior Member
 
Registered: Apr 2009
Location: McKinney, Texas
Distribution: Slackware64 15.0
Posts: 3,858

Rep: Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225Reputation: 2225
Quote:
Originally Posted by kikinovak View Post
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:

Code:
# Command prompt for non-root users
GREEN='\[\033[0;32m\]'
WHITE='\[\033[1;37m\]'
NC='\[\033[0;m\]'
PS1="$GREEN[$WHITE\u$NC@$WHITE\h$NC:$WHITE\W$GREEN] \$ $NC"
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.
 
2 members found this post helpful.
Old 10-20-2015, 12:24 AM   #5
a4z
Senior Member
 
Registered: Feb 2009
Posts: 1,727

Rep: Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742Reputation: 742
Quote:
Originally Posted by kikinovak View Post
Thanks, ponce.

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
 
Old 10-20-2015, 01:30 AM   #6
kikinovak
MLED Founder
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: CentOS, OpenSUSE
Posts: 3,453

Original Poster
Rep: Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154
Quote:
Originally Posted by a4z View Post
I think this will double your PATH, just to mention one negative side effect
PS1 in bashrc is ok, as aliases
No, PATH is not doubled. Tried it both in login shells and non-login shells.
 
Old 10-20-2015, 01:53 AM   #7
kikinovak
MLED Founder
 
Registered: Jun 2011
Location: Montpezat (South France)
Distribution: CentOS, OpenSUSE
Posts: 3,453

Original Poster
Rep: Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154Reputation: 2154
Quote:
Originally Posted by a4z View Post
I think this will double your PATH, just to mention one negative side effect
PS1 in bashrc is ok, as aliases
Here's a more orthodox solution though, which works in all shells, login and non-login. Simply move PATH from .bashrc to .bash_profile:

Code:
# $HOME/.bash_profile

# PATH
PATH=$PATH:/sbin

# $HOME/.bashrc
if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi
All the other stuff (PS1, aliases, EDITOR, VISUAL) goes in .bashrc.
 
1 members found this post helpful.
Old 10-20-2015, 02:05 AM   #8
dunne
Member
 
Registered: May 2014
Distribution: OpenBSD
Posts: 67

Rep: Reputation: 36
Quote:
But when I launch WindowMaker and open XTerm
Xterms are login shells by default, so they won't read .bash_profile. To make them login shells, put:

Code:
XTerm*loginShell:true
in
Code:
.Xdefaults
.
 
  


Reply



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
[SOLVED] /etc/bashrc ,dircolors and /root/.bash_profile ,/.bashrc not exist in LFS-7.5(sec9.3) jaassi Linux From Scratch 2 09-20-2014 05:04 PM
/home/users/.bash_profile Permisson Deny shaana2khan Linux - Newbie 3 10-09-2013 07:30 AM
[SOLVED] How to use my /home/kangjoo/.bashrc rather than root/.bashrc kangjoo.lee Linux - Newbie 2 11-05-2012 03:38 PM
contents of 'home' directory found at sda5: mounted as /home, and also on sda6:/home leswatson Linux - Newbie 4 04-18-2008 04:02 PM
/etc/profile VERSUS /home/.bash_profile carbono Linux - Newbie 12 10-12-2005 09:23 AM

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

All times are GMT -5. The time now is 11:49 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
Open Source Consulting | Domain Registration