Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers 2

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:

PROMPT_COMMAND='
  myhost=$(hostname)
  if [[ $myhost == master ]]; then
    myhost=${myhost^^}
  fi
'

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.

share|improve this answer
    
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
add comment

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

$(cmd)

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.