ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would like to put a variable in the $PS1 prompt that will change each time a command is run. I want the color of the $PS1 prompt to change each time a command is run.
I know that I can do this:
Code:
PS1="\h@\w \# "
## "\#" is changes every time a command is run
But I would like to do this: ( but it does not work )
Code:
var=$(for var in 1 2 3 4 5 6 ; do echo $var; done)
PS1="\[\033\[\4"$var";3m\] \h@\w \[\033[00m\] "
It works but do you know how to connect the change in colour to this "\!" ? That is the history sequence. That way the colour will only change if a command is actually run. Right now the colour changes anytime I press the "Enter" key.
Well, there is the environment variable HISTCMD which gives the number of the actual entry in the history file and one can check whether it changed. Unfortunately (and nevertheless interesting) this variable always resolves to 1 inside the PROMPT_COMMAND statement(s).
PROMPT_COMMAND='cmd="$(tail -1 ~/.bash_history)"; history -a; if [[ "$(tail -1 ~/.bash_history)" != "$cmd" ]]; then let "var=++var>6?1:var"; fi; PS1="\[\e[0;3${var}m\]\u@\h:\w> \[\e[m\]"'
This changes colour only if you enter a command different from the previous one. Pressing enter alone has no effect (colour doesn't change). The history -a command append the new history line to the history file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.