Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing bash prompt in new bash

Tags:

bash

shell

prompt

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!

like image 247
Jonny5 Avatar asked Apr 16 '12 17:04

Jonny5


2 Answers

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.

like image 196
Michael Wild Avatar answered Sep 20 '22 23:09

Michael Wild


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$ 
like image 30
sdaau Avatar answered Sep 17 '22 23:09

sdaau