LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 01-29-2010, 07:08 PM   #1
ash_zz_00
Member
 
Registered: Aug 2009
Distribution: Slackware 13.0
Posts: 66

Rep: Reputation: 17
Slackware 13.0 ~/.bash_profile not running at login


Hi,
I'm logging in as a user at run level 3.

I have my .bash_profile in my home directory
Code:
if [ -f ~/.bashrc ]; then
    source ~/.bashrc
fi
Then my .bashrc has the following
Code:
echo "Running .bashrc..."
export PS1="[\w]\\$ "
echo "Done"
When I log in or start a new shell (terminal/xterm) in X, I expected to see the echo and directory at the shell prompt. But it doesn't.
However, if I explicitly 'source' the .bash_profile it works.
What am I doing wrong?

Thanks,

Ash.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 01-29-2010, 10:45 PM   #2
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Ok, here's what the bash man pages have to say on the subject:

Code:
INVOCATION
       A  login shell is one whose first character of argument zero is a -, or
       one started with the --login option.

       An interactive shell is one started without  non-option  arguments  and
       without the -c option whose standard input and error are both connected
       to terminals (as determined by isatty(3)), or one started with  the  -i
       option.   PS1 is set and $- includes i if bash is interactive, allowing
       a shell script or a startup file to test this state.

       The following paragraphs describe how bash executes its startup  files.
       If  any  of  the files exist but cannot be read, bash reports an error.
       Tildes are expanded in file names as described below under Tilde Expan‐
       sion in the EXPANSION section.

       When  bash is invoked as an interactive login shell, or as a non-inter‐
       active shell with the --login option, it first reads and executes  com‐
       mands  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 /etc/bash.bashrc and ~/.bashrc, if
       these files exist.  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 /etc/bash.bashrc and ~/.bashrc.
Opening a new terminal in X invokes bash as an interactive shell, but not as a login shell. Therefore your ~/.bash_profile will not execute, but your ~/.bashrc should.

It sounds to me like bash is not your default shell.

Try

Code:
echo $SHELL
and

Code:
grep <your username> /etc/passwd | cut -d ':' -f 7
The other question that might be worth asking is what window manager you're using... back when I used slackware, I used fvwm, which had a text based config file. If the shell is launched from one of the menus, it's possible that it's invoking either bash or some other shell in a strange way, based on the config file... that's a streach, I know, but worth a shot.
 
2 members found this post helpful.
Old 01-30-2010, 03:06 PM   #3
ash_zz_00
Member
 
Registered: Aug 2009
Distribution: Slackware 13.0
Posts: 66

Original Poster
Rep: Reputation: 17
Hi bartonski,

Thank you very much for you suggestions. It made me look around and I finally resolved it by explicitly setting the shell.
Code:
usermod -s /bin/bash <username>

sh-3.1$ grep <username> /etc/passwd | cut -d ':' -f 7
/bin/bash
But I don't quite understand what was going on before.
From what I could tell, it looked like I was in bash.
I am using Xfce for the window manager. I also switched to kde but that didn't make any difference.

The following is some information before I fixed it.
Code:
sh-3.1$ echo $SHELL
/bin/sh

sh-3.1$ ls -l /bin | grep bash
-rwxr-xr-x 1 root root 678832 2007-05-10 18:19 bash
lrwxrwxrwx 1 root root      4 2010-01-17 15:41 sh -> bash

sh-3.1$ grep <username> /etc/passwd | cut -d ':' -f 7
<didn't find anything>
But I thought the default in Slackware was bash if it wasn't specified.
What seems a little odd though was the value of my $PS1.

From the man pages
Quote:
...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 ...
Code:
/etc/profile 

# Set a default shell prompt:
#PS1='`hostname`:`pwd`# '
if [ "$SHELL" = "/bin/pdksh" ]; then
 PS1='! $ '
elif [ "$SHELL" = "/bin/ksh" ]; then
 PS1='! ${PWD/#$HOME/~}$ '
elif [ "$SHELL" = "/bin/zsh" ]; then
 PS1='%n@%m:%~%# '
elif [ "$SHELL" = "/bin/ash" ]; then
 PS1='$ '
else
 PS1='\u@\h:\w\$ '
fi
So even if my ~/.bash_profile and ~/.bashrc was not being read for whatever reason, I would have expected $PS1=\u@\h:\w\$ . But it was different. Where could that have been coming from?

Code:
sh-3.1$ echo $PS1
\s-\v\$
Thanks,

Ash.

Last edited by ash_zz_00; 01-30-2010 at 03:15 PM. Reason: Fixed typo in code section
 
1 members found this post helpful.
Old 01-30-2010, 04:01 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,978
Blog Entries: 11

Rep: Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879Reputation: 879
The "problem" is that, once you're logged in (into X, in this case)
all subsequently invoked shells aren't login shells, so all
that gets invoked for them is ~/.bashrc.
 
1 members found this post helpful.
Old 01-30-2010, 04:25 PM   #5
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by ash_zz_00 View Post
sh-3.1$ grep <username> /etc/passwd | cut -d ':' -f 7
<didn't find anything>
Oh, sorry... you need to put your actual username where it says <username>. You don't actually need to pipe it through cut, that just shows you the correct field in the password file.

Quote:
Originally Posted by ash_zz_00 View Post
But I thought the default in Slackware was bash if it wasn't specified.
What seems a little odd though was the value of my $PS1.

From the man pages

Quote:
...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 ...
Code:
/etc/profile 

# Set a default shell prompt:
#PS1='`hostname`:`pwd`# '
if [ "$SHELL" = "/bin/pdksh" ]; then
 PS1='! $ '
elif [ "$SHELL" = "/bin/ksh" ]; then
 PS1='! ${PWD/#$HOME/~}$ '
elif [ "$SHELL" = "/bin/zsh" ]; then
 PS1='%n@%m:%~%# '
elif [ "$SHELL" = "/bin/ash" ]; then
 PS1='$ '
else
 PS1='\u@\h:\w\$ '
fi
So even if my ~/.bash_profile and ~/.bashrc was not being read for whatever reason, I would have expected $PS1=\u@\h:\w\$ . But it was different. Where could that have been coming from?

Code:
sh-3.1$ echo $PS1
\s-\v\$
Thanks,

Ash.
If you go upwards in the man pages a scoche, you'll find that this section refers only to login shells. Essentially, this means that if you have to put your username and password in in order to access the shell its self, you'll be executing /etc/profile, ~/.bash_profile, ~/.bash_login, etc... however, you're launching from within X. If you're running X, you're not running a login shell. Either you've logged in to X, via xdm, or logged in to another shell, then launched X via startx or xsession or something like that. As such from X, you're launching an interactive shell, but not a login shell. An interactive bash shell which is not a login shell will call ~/.bashrc.
 
1 members found this post helpful.
Old 01-30-2010, 05:16 PM   #6
ash_zz_00
Member
 
Registered: Aug 2009
Distribution: Slackware 13.0
Posts: 66

Original Poster
Rep: Reputation: 17
Quote:
Oh, sorry... you need to put your actual username where it says <username>. You don't actually need to pipe it through cut, that just shows you the correct field in the password file.
I did put in my username and I checked the passwd file too. That field was empty.

So it made me wonder how I created the user account. To test it, I ran the "adduser" command. After a number of prompts, it asked for the shell (with /bin/bash as the default). When I grep'd the passwd file, it had the /bin/bash entry.

What could I have done because mine didn't have it? Then I realized that I might have run "useradd" instead. So I tested with it. Indeed, the shell entry is blank.

Quote:
If you go upwards in the man pages a scoche, you'll find that this section refers only to login shells.
Actually I had the same problem with the shell as soon as I logged on (which would be the login shell right?). I don't use xdm/kdm to log in.

Quote:
As such from X, you're launching an interactive shell, but not a login shell. An interactive bash shell which is not a login shell will call ~/.bashrc.
So if I don't have a ~/.bashrc or for some reason my .bashrc is not being invoked, do you know where the interactive shell gets its defaults?

Again, thanks for you help. I'm asking all these questions just to understand better.

Regards,

Ash.

PS: By the way how do I get the "Originally Posted by ..." into the quote. Is that a manual copy/paste?
 
Old 01-31-2010, 01:04 AM   #7
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,546
Blog Entries: 28

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by ash_zz_00 View Post
So if I don't have a ~/.bashrc or for some reason my .bashrc is not being invoked, do you know where the interactive shell gets its defaults?
The GNU Bash Reference explains.
Quote:
Originally Posted by ash_zz_00 View Post
PS: By the way how do I get the "Originally Posted by ..." into the quote. Is that a manual copy/paste?
It "just works" for me on clicking the Quote button to reply.
 
Old 01-31-2010, 01:05 PM   #8
ash_zz_00
Member
 
Registered: Aug 2009
Distribution: Slackware 13.0
Posts: 66

Original Poster
Rep: Reputation: 17
Thank you!
 
Old 02-01-2010, 12:43 AM   #9
bartonski
Member
 
Registered: Jul 2006
Location: Louisville, KY
Distribution: Fedora 12, Slackware, Debian, Ubuntu Karmic, FreeBSD 7.1
Posts: 443
Blog Entries: 1

Rep: Reputation: 47
Quote:
Originally Posted by ash_zz_00 View Post
I did put in my username and I checked the passwd file too. That field was empty.
That would explain why .bashrc never ran. I'm not 100% sure what happens when the shell field is not set, but my guess is that /bin/sh gets called.

/bin/sh is typically a symbolic link to another shell. Which shell gets called varies from distro to distro. Usually this will either be ash or bash, although Debian uses a shell called dash. Here's the kicker: if called as /bin/sh, each of these shells will be called as a POSIX shell, meaning that they only execute the POSIX subset of shell commands. This is important for portability concerns. Among other things, this means that even if the symlink for /bin/sh points to /bin/bash, .bashrc won't be called for a non-login interactive shell, and .bash_profile won't be called for login shells.
 
Old 03-09-2012, 01:58 PM   #10
ysawej
LQ Newbie
 
Registered: Mar 2012
Posts: 1

Rep: Reputation: Disabled
.profile is the old standard that bash uses for setting its environment variables. So create one (.profile) if it does not exist in your home directory. As bartonski has mentioned, /bin/sh does not call .bash_profile. So you will have to manually call .bash_profile inside .profile
 
  


Reply

Tags
bash, bashrc, profile


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
~/.bash_profile not being executed at login Radicalm16 Slackware 9 07-28-2009 09:39 PM
.bash_profile not executed at login Kiwi944 Linux - Newbie 6 06-25-2009 07:22 AM
Cannt login as root . made changes to .bash_profile hotbacteria Linux - General 5 02-01-2006 03:24 AM
Cannt login as root . made changes to .bash_profile hotbacteria Linux - Newbie 3 01-31-2006 10:02 PM
.bash_profile overwritten on login to root Awesome Dude Linux - Newbie 1 08-23-2004 02:41 PM


All times are GMT -5. The time now is 03:21 PM.

Main Menu
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