1

In a shared cluster I work on, the environment settings for new accounts does not include a prompt setting, as far as I can tell. In particular, PS1 is not set. And yet, when one logs into a new account, one gets a rather fancy prompt, featuring the username and the basename of the current directory.

I want to save this prompt (e.g. in an environment variable like ORIGINAL_PS1) before replacing it with my preferred prompt, so that I can restore it later if desired1.

How can I extract the prompt definition from the current prompt?

I am primarily interested in the answer to this question for bash, but if there's something analogous for zsh, please let me know.


1 I rather like the default prompt, but for everyday use I prefer to use a "git-aware" prompt. That said, although I find the latter extremely helpful 99.999% of the time, ocassionally I cd to a git repo that is sufficiently messed up that it causes the prompt's generation to become unacceptably slow. For those rare circumstances, I'd like to be able to run export PS1=$ORIGINAL_PS1.

4
  • 2
    Does ORIGINAL_PS1=$PS1 work for you? – fd0 Aug 22 '19 at 12:11
  • 2
    Just to comment on the very last bit of the question: PS1 never has to be exported. It's purely a shell variable and no child processes of the shell needs to inherit it. – Kusalananda Aug 22 '19 at 12:13
  • 2
    If PS1 is not set, then that's probably not bash. what does echo $prompt say? – mosvy Aug 22 '19 at 12:14
  • @Kusalananda: That observation actually solved the mystery: I had looked at the output of printenv for the prompt's definition, mistakingly thinking that it was exported. Thank you all. All your comments nailed it, each in a different way! – kjo Aug 22 '19 at 12:22
2

ORIGINAL_PS1=$PS1 would save the value of the primary prompt variable to a new variable.

The PS1 variable is a shell variable that is not exported, i.e. it's not an environment variable that is inherited by child processes.

In the zsh shell, you may want to additionally save the "right primary prompt", RPS1, if that is used. The primary prompt is called PS1 in zsh, just as in bash.

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.