7

I just spent a lot of time reading up on login and interactive shells and why one should or should not set environment variables, shell functions, etc. in the various profile and bashrc files. In this post it was mentioned that bash specific things like prompt options should be set in ~/.bashrc. That led me to wonder about the PS1 variable. In all the examples I've seen regarding this they have something like export PS1="". Should this really be exported to the environment since it only has meaning for bash? Just having PS1="" in my ~/.bashrc produces the intended effect for me, but I'm wondering if I'm missing something.

13

That's correct: PS1 is only meaningful in interactive instances of bash, so it should be set in ~/.bashrc and should not be exported. PS1 is also meaningful in other shells, but it has a different meaning, because prompt expansions differ between shells. In fact, even between instances of bash, PS1 can have different meanings, since the meaning depends on shell options (at least promptvars).

Exporting PS1 to the environment from .profile is a throwback to the 1970s, when there was only one shell that used it (the Bourne shell) and it didn't have a configuration file. It still works today if you always use the same shell and never configure it differently. But all modern shells that aren't designed purely for scripting (csh, ksh, bash, zsh, …) read a configuration file when started interactively (.cshrc, .kshrc, .bashrc, .zshrc, …), so the 1970s method is no longer necessary. Setting PS1 and other shell-specific settings in a shell-specific file, and not exporting it to the environment, avoids breaking things when you use a different shell configuration or a different shell or a different terminal that isn't capable of showing your usual prompt fanciness. Setting PS1 in a shell-specific file works all the time, whereas setting it in .profile and exporting it only works in “simple” cases, so there's no reason not to do it the right way, but there are plenty of bad tutorials around the web and even bad default configurations in distributions. C'est la vie.

  • How does exporting PS1 from .profile work for non-login bash shells since they wouldn't source it? Are you saying this would work because the non-login shell would be forked from a login shell so would inherit PS1 through the environment? – Mike Sweeney Dec 6 '15 at 2:33
  • @MikeSweeney yes, that's why it's exported. – muru Dec 6 '15 at 2:53
  • One way that the wrong idea spreads about exporting PS1 is from seeing if [ -n "$PS1" ] ; then proceed assuming an INTERACTIVE shell ; fi -- which commonly appears in people's .bashrc files to only load things like command-completion if this is a user's interactive terminal shell. So, seeing it work there, the same logic winds up in shell scripts, hence the "need" to export it. INSTEAD we should check for interactive / terminal users with tty -s or test -t 0. – DouglasDD Jul 12 '17 at 1:50
  • 1
    @DouglasDD Indeed. The PS1 test has been in Debian's /etc/profilefor ages, for example. I don't know where this bad practice arose. I suspect it came from one particular use case (maybe detecting rlogin or ssh logins?) where it happened to work. Unfortunately it fails in many other cases, hence the many questions on this topic here and elsewhere. – Gilles Jul 13 '17 at 16:04
  • @Gilles should we not export CLICOLOR and LS_COLORS either? @DouglasDD does that mean the if [[-z $p1]];then return fi check in my .bashrc is flawed? – Startec Sep 1 '17 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.