Suppose I have an environment variable defined in .bash_profile like this:

PS1='\[\e[31;1m\][\u@\h \T \w] \[\e[0m\]'

This variable makes my Linux prompt show certain information, like thes:

[jeff@node21 01:48:30 ~] [jeff@node22 01:48:30 ~] [jeff@node23 01:48:30 ~] [jeff@master 01:48:30 ~]

When the word "master" appears, I want to capitalize it (I am not to do any work on that node and I want it to stand out when I forget to rsh to other nodes).

I can't use sed on the variable PS1, because that doesn't contain the word "master" in it.

Ultimately, I want the prompt to be capitalized when I'm on node "master". Is there a way to evaluate variable "PS1", manipulate the string, and then re-assign it so the prompt works?


Assuming you're using a recent version of bash, you can accomplish this with PROMPT_COMMAND and using a variable in PS1 instead of \h:

  if [[ $myhost == master ]]; then

PS1='\[\e[31;1m\][\u@$myhost \T \w] \[\e[0m\]'

Whatever is in PROMPT_COMMAND is run before every prompt is written. The above definition sets myhost to the output of hostname, and if myhost is master, it gets uppercased by the ${var^^} expansion.

  • In Bash versions older than 4, to capitalize use tr '[a-z]' '[A-Z]' – westeros91 Aug 12 '12 at 16:49
  • or tr '[:lower:]' '[:upper:]' -- man.cx/tr – glenn jackman Aug 12 '12 at 23:57

an alternative approach would be

PS1='\[\e[31;1m\][\u@$(hostname | perl -pe "\$_ = uc if m/master/") \T \w] \[\e[0m\]'

i admit that my suggestion is not so well structured like Thor's one, but it's basically nice to know that


is the syntax for command substitution, like generally in bash.

There is also a nice howto which only deals with the configuration of bash's prompt. It's definitely worth reading.

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.