Changing bash prompt in new bash Changing bash prompt in new bash shell shell

Changing bash prompt in new bash


The prompt is defined by the PS1, PS2, PS3 and PS4 environment variables. So, e.g. the following will start a new bash with the prompt set to "foo: ":

PS1="foo: " bash --norc

The --norc is required to suppress processing of the initialization files, which would override the PS1 variable.


I have the same problem - I'd like to startup a temporary bash from the command line; and while most other environment variables remain; those that are sourced from ~/.bashrc are kind of difficult to override - especially if you, like me, would actually like to keep the ~/.bashrc you already have (and aliases inside, etc.) - save for the PS1 prompt.

Here is something that works for me (note that --init-file is a synonym/alias for --rcfile):

bash --rcfile <(cat ~/.bashrc ; echo 'PS1="\[\033[0;33m\]\u@HELLO:\W\$\[\033[00m\] "')

Basically, the bracket/less-than + parenthesis idiom <() starts up bash process substitution; everything echoed to stdout inside the parenthesis will end up in a temporary file, /dev/fd/<n>. So we first cat the contents of out ~/.bashrc; then we simply add a PS1 set command at end, (which effectively overrides) - this ends up in /dev/fd/<n>; and bash then uses /dev/fd/<n> for the new rcfile.

This is how it behaves:

user@pc:tmp$ TESTVAR="testing" bash --rcfile <(cat ~/.bashrc ; echo 'PS1="\[\033[0;33m\]\u@HELLO:\W\$\[\033[00m\] "')user@HELLO:tmp$ test-alias-tab-completion ^Cuser@HELLO:tmp$ echo $TESTVAR testinguser@HELLO:tmp$ exitexituser@pc:tmp$ 


You can set an environment variable, and then use that environment variable in your prompt in .bashrc.