LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Slackware 13.0 ~/.bash_profile not running at login (https://www.linuxquestions.org/questions/linux-newbie-8/slackware-13-0-%7E-bash_profile-not-running-at-login-785720/)

ash_zz_00 01-29-2010 07:08 PM

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.

bartonski 01-29-2010 10:45 PM

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.

ash_zz_00 01-30-2010 03:06 PM

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.

Tinkster 01-30-2010 04:01 PM

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.

bartonski 01-30-2010 04:25 PM

Quote:

Originally Posted by ash_zz_00 (Post 3846458)
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 (Post 3846458)
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.

ash_zz_00 01-30-2010 05:16 PM

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?

catkin 01-31-2010 01:04 AM

Quote:

Originally Posted by ash_zz_00 (Post 3846581)
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 (Post 3846581)
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.

ash_zz_00 01-31-2010 01:05 PM

Thank you!

bartonski 02-01-2010 12:43 AM

Quote:

Originally Posted by ash_zz_00 (Post 3846581)
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.

ysawej 03-09-2012 01:58 PM

.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


All times are GMT -5. The time now is 07:51 AM.