k den
Posts: 8
Joined: Sun Dec 27, 2015 3:09 pm

~/.bash_profile runs only thru SSH (not GUI)

Sun Dec 27, 2015 11:32 pm

Hey guys,

I've been trying to run some code automatically at the beginning of every terminal session using the file ~/.bash_profile, but for some reason the code only runs when I access the terminal interactively (in other words, through SSH.) The code goes something like this:

Code: Select all

echo " "
echo "Welcome, Master."
echo "You may begin your terminal session below."
source ~/.bashrc
Note: The source ~/.bashrc is only there to pull the color for the PS1 variable, since I changed it from the original green and blue.
If someone could tell me what's going on, that would be great! :D Thanks!

User avatar
solar3000
Posts: 1051
Joined: Sat May 18, 2013 12:14 am

Re: ~/.bash_profile runs only thru SSH (not GUI)

Mon Dec 28, 2015 12:55 am

Have you tried the code in .bashrc?

You can also type: exec bash -l

Or: /bin/sh -l
Antikythera

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: ~/.bash_profile runs only thru SSH (not GUI)

Mon Dec 28, 2015 1:17 am

k den wrote:Hey guys,

I've been trying to run some code automatically at the beginning of every terminal session using the file ~/.bash_profile, but for some reason the code only runs when I access the terminal interactively (in other words, through SSH.)
You are saying that it only runs when you do something that requires you to login? If you look at the ~/.profile file, you will likely see that it says that it doesn't run if .bash_profile exists but that it is also only going to run if it is a login shell.

When I put

echo "This is a login shell"

in the .profile file, it echoes that text when I ssh in but it doesn't when I just su user into that account. Then I added to the .bashrc file the following and tested again:

echo "We are now in .bashrc"

When I did the su, I *only* got the part about being in .bashrc but when I ssh, I get *both* strings. You'll want to experiment with all this because apparently it isn't perfectly consistent under every unix like operating system and you can source different files to make it behave however. Including an echo line saying what file is being shown might be useful in repeatedly giving you clues as to which is going to run when you do which type of account use.

k den
Posts: 8
Joined: Sun Dec 27, 2015 3:09 pm

Re: ~/.bash_profile runs only thru SSH (not GUI)

Mon Dec 28, 2015 3:30 am

Alright, I'll be sure to check that out and see if it works. I'll post back with my result. Thanks for the help!

k den
Posts: 8
Joined: Sun Dec 27, 2015 3:09 pm

Re: ~/.bash_profile runs only thru SSH (not GUI)

Mon Dec 28, 2015 2:56 pm

So I put an echo in ~/.bash_profile, ~/.bashrc, and ~/.profile that tells me which code is running upon startup. I found that through SSH both ~/.bash_profile and ~/.bashrc run. However, when running a non-interactive terminal, only ~/.bashrc runs. Would it work to just call a source of ~/.bash_profile within ~/.bashrc? Images are below. :)
Attachments
Screenshot 2015-12-28 at 8.48.25 AM.png
GUI (through a VNC server)
Screenshot 2015-12-28 at 8.48.25 AM.png (3.6 KiB) Viewed 1697 times
Screenshot 2015-12-28 at 8.47.00 AM.png
SSH
Screenshot 2015-12-28 at 8.47.00 AM.png (20.6 KiB) Viewed 1697 times

QuietZone
Posts: 89
Joined: Sat Dec 05, 2015 7:13 pm

Re: ~/.bash_profile runs only thru SSH (not GUI)

Mon Dec 28, 2015 11:36 pm

You need to keep in mind that this is all working as designed.

When you open a terminal from within the GUI, you aren't logging in. You've already logged in (a long time ago). Opening a terminal from within the GUI is like "shelling out" from an application (in fact, it is exactly that). And when you shell out of an application (e.g., vi), it doesn't run any of the "profile" type files - it only runs the "rc" type files. This is all pretty standard Unix practice.
"If you haven't got anything nice to say about anybody come sit next to me." — Alice Roosevelt Longworth

Return to “General discussion”