'Changing bash prompt in new bash
When I create an new bash process, the prompt defaults to a very simple one. I know I can edit .bashrc etc to change this, but is there a way of passing the prompt with the bash command?
thanks!
Solution 1:[1]
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.
Solution 2:[2]
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 ^C
user@HELLO:tmp$ echo $TESTVAR
testing
user@HELLO:tmp$ exit
exit
user@pc:tmp$
Solution 3:[3]
You can set an environment variable, and then use that environment variable in your prompt in .bashrc.
Solution 4:[4]
If you want the profiles, but don't want to hardcode all the possible paths to source them from, then a more generic somewhat shell-agnostic solution is:
PROMPT_COMMAND='PS1="(customize) $PS1"; PROMPT_COMMAND=' $SHELL
With the caveat of only working as long as the default profiles don't set PROMPT_COMMAND
. But that's more likely than them not setting PS1
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Michael Wild |
Solution 2 | |
Solution 3 | Ned Batchelder |
Solution 4 | tambre |