Jonny5
Jonny5

Reputation: 1499

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!

Upvotes: 7

Views: 3553

Answers (4)

tambre
tambre

Reputation: 4843

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.

Upvotes: 0

sdaau
sdaau

Reputation: 38619

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$ 

Upvotes: 5

Michael Wild
Michael Wild

Reputation: 26331

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.

Upvotes: 13

Ned Batchelder
Ned Batchelder

Reputation: 375484

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

Upvotes: 0

Related Questions