How to change Bash prompt for login shell?

versatile 64-bit multi-user Linux distribution

Moderators: kirk, jamesbond, p310don, JakeSFR, step, Forum moderators

Post Reply
User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

How to change Bash prompt for login shell?

Post by m1k3 »

I hate to ask this because I feel like I'm missing something obvious. When I changed the shell to bash for root, I get the default bash prompt (bash-5.2# ). No problem, right - just change it in /etc/profile or even ~/.profile. Heck, maybe even add a custom prompt to /etc/shinit or ~/.shinit for good measure too. None of that worked though. The only thing that worked was adding the prompt to ~/.bashrc.

So what am I missing? How do I get the prompt to change for the login shell and not the interactive shell?

User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

Re: How to change Bash prompt for login shell?

Post by m1k3 »

Ok, I think I might be getting somewhere. If I export the prompt in /etc/profile then it does in indeed change the login prompt when changing to another TTY. So where does it get reset? Hmm....

Imran
Posts: 4
Joined: Thu Jun 23, 2022 10:50 pm
Has thanked: 1 time

Re: How to change Bash prompt for login shell?

Post by Imran »

You're getting confused between login & interactive shells (don't blame you - it's needlessly complicated for maximum flexibility).

profile or .bash_profile only get rerun if it's a login shell (without looking at your config, perhaps that's why you are only seeing your profile changes when opening a new vt)

You can test this more conveniently by configuring a separate menu entry to get your favourite terminal to run bash as a login shell.

But I really wouldn't put your prompt stuff into your profile. Even if a bash instance is running as a login shell, it will still run your bashrc if it's ALSO an interactive shell. The 2 are not mutually exclusive.

I look at the whole profile vs bashrc thing a little differently. For me, profile creates the environment which my bash scripts can rely on (so prompt is superfluous, also - profile shouldn't output anything to stderr or stdout). bashrc is for my terminals (go crazy, change term colourscheme on the fly, nice prompts, list out your PATH and a custom motd, Throw in a joke or 2 from your fortune file. Whatever ...)

step
Posts: 510
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 179 times
Contact:

Re: How to change Bash prompt for login shell?

Post by step »

User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

Re: How to change Bash prompt for login shell?

Post by m1k3 »

Thank you both!

@Imran, That's a good way to look at it. It surprising because advice on the net is all over the place in terms of using profile vs bashrc. I like your approach though.

@step, I tried ~/.fatdog/profile as well but it was the same result as putting it directly in /etc/profile which sources ~/.fatdog/profile toward the end of the file. It changes the prompt just fine in a TTY but in a vt in an X session it goes back to the default bash prompt unless I have it in .bashrc.

Imran
Posts: 4
Joined: Thu Jun 23, 2022 10:50 pm
Has thanked: 1 time

Re: How to change Bash prompt for login shell?

Post by Imran »

@m1k3

in response to what you said to @step , that's because your terminal is probably running a non-login, interactive bash. So it never even tries to source profile. Your new bash shell has the original profile which was executed in the great-great-great-grandfather bash process which invoked Xorg. Your new bash shell only ran bashrc .

You can see the difference as follows: in your new bash terminal, now run 'bash -l', to get a new login & interactive bash instance. See? Your just-edited profile changes will show up then.

The trailing points in my previous post still apply however. HTH :-)

User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

Re: How to change Bash prompt for login shell?

Post by m1k3 »

True, I had noticed when I used the -l argument yesterday but didn't make the connection. I'll stick to .bashrc as you recommended. Thanks! :thumbup:

jamesbond
Posts: 534
Joined: Tue Aug 11, 2020 3:02 pm
Location: The Pale Blue Dot
Has thanked: 71 times
Been thanked: 291 times

Re: How to change Bash prompt for login shell?

Post by jamesbond »

Just want to add my late opinion on this.

@m1k3, then you said you want to run bash as the shell, what do you mean? Because by default, the login shell (and any other shell you run inside the terminal emulators) are all bash shell already. /bin/sh is a symlink to /bin/bash. Bash does run slightly differently when it is invoked as "sh" or as "bash" (e.g. the startup files confusion you have), but otherwise almost all of bash features are exposed as well, even when if it is invoked as "sh".

In my opinion, the only place where you want to explicitly run as "bash" is when you're writing a script that needs bash-specific features (also called as "bashisms"). In this case, even if the scripts works with #!/bin/sh or #!/bin/bash (again, because /bin/sh is a symlink to /bin/bash), it's better to put #!/bin/bash so you remember that this scripts needs bash's features and won't work with other shells.

That, or when you need specific features of bash that is only enabled when bash is invoked as "bash" and not "sh" (I can't remember offhand what are these features).

User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

Re: How to change Bash prompt for login shell?

Post by m1k3 »

@jamesbond: Oh man, now I feel dumb. :lol: I didn't even think to see what sh was symlinked to and assuming it was dash. Thanks for the info! I'll keep my shell setup in .shinit then.

step
Posts: 510
Joined: Thu Aug 13, 2020 9:55 am
Has thanked: 50 times
Been thanked: 179 times
Contact:

Re: How to change Bash prompt for login shell?

Post by step »

m1k3 wrote: Sat Jan 20, 2024 7:41 pm

I'll keep my shell setup in .shinit then.

I prefer not to because I use both shells interactively, dash and bash (and sh). They all read .shinit. If bashis are plcced in .shinit they will make dash complain or error out. So I put bashisms in bashrc and the rest can go into .shinit.

Of course, you can keep things very simple by just using bash and keeping your settings in one file. Settings for interactive shells can be guarded by a test on the shell -i flag.

User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

Re: How to change Bash prompt for login shell?

Post by m1k3 »

@step Ah, that's a good point - thank you.

williwaw
Posts: 1592
Joined: Tue Jul 14, 2020 11:24 pm
Has thanked: 145 times
Been thanked: 291 times

Re: How to change Bash prompt for login shell?

Post by williwaw »

User avatar
m1k3
Posts: 86
Joined: Sat Sep 26, 2020 1:44 am
Has thanked: 33 times
Been thanked: 11 times

Re: How to change Bash prompt for login shell?

Post by m1k3 »

Thank @williwaw, that's a nice flowchart.

Post Reply

Return to “FatDog64”