LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2011, 08:08 PM   #1
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Rep: Reputation: 18
BASH $PS1 Hack


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\] "
Does this make sense to you guys/girls?
 
Old 02-11-2011, 09:01 PM   #2
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
perhaps it should be:

Code:
PS1=$'\[\033\[\4'$var$';3m\] \h@\w \[\033[00m\] '
 
Old 02-11-2011, 09:13 PM   #3
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
hmmmm........

I'll try that.....

Quote:
Originally Posted by konsolebox View Post
perhaps it should be:

Code:
PS1=$'\[\033\[\4'$var$';3m\] \h@\w \[\033[00m\] '
 
Old 07-17-2011, 03:22 PM   #4
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
?

konsolebox:

Your suggestion did not work.
 
Old 07-17-2011, 03:58 PM   #5
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
The var assignment you used will not create an array but just one variable. If you would like to use an array you could use:
Code:
$ var=(1 2 3 4 5 6)
But you don’t it here anyway. To change the color with each return, it needs to be put in PROMPT_COMMAND:
Code:
$ PROMPT_COMMAND='let var++; if [ "$var" -gt 6 ]; then var=1; fi; PS1="\[\e[0;3${var}m\]\u@\h:\w> \[\e[m\]"'

Last edited by Reuti; 07-17-2011 at 04:20 PM. Reason: Insert explaining text between the code lines
 
Old 07-17-2011, 04:11 PM   #6
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
Add-on to skip the if:
Code:
$ PROMPT_COMMAND='let "var=++var>6?1:var"; PS1="\[\e[0;3${var}m\]\u@\h:\w> \[\e[m\]"'

Last edited by Reuti; 07-17-2011 at 04:15 PM.
 
Old 07-17-2011, 04:24 PM   #7
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
Oh my goodness!

Reuti:

THANK YOU!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Quote:
Originally Posted by Reuti View Post
Add-on to skip the if:
Code:
$ PROMPT_COMMAND='let "var=++var>6?1:var"; PS1="\[\e[0;3${var}m\]\u@\h:\w> \[\e[m\]"'
I just could not figure it out. I was trying to do it another more complex way. Well, we learn something new everyday.
 
Old 07-17-2011, 06:21 PM   #8
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
question

Reuti:

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.

Quote:
Originally Posted by Reuti View Post
Add-on to skip the if:
Code:
$ PROMPT_COMMAND='let "var=++var>6?1:var"; PS1="\[\e[0;3${var}m\]\u@\h:\w> \[\e[m\]"'
 
Old 07-18-2011, 04:33 AM   #9
Reuti
Senior Member
 
Registered: Dec 2004
Location: Marburg, Germany
Distribution: openSUSE 15.2
Posts: 1,339

Rep: Reputation: 260Reputation: 260Reputation: 260
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).

So, for now I have no idea how to achieve it.
 
Old 07-18-2011, 05:01 AM   #10
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Code:
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.
 
Old 10-02-2011, 08:30 PM   #11
cryingthug
Member
 
Registered: Jun 2009
Posts: 131

Original Poster
Rep: Reputation: 18
Solved

I came up with this with the help of you guys.

Code:
let n=232

    _cmd(){
        if [ "$n" -lt 255 ] ; then
            let n++
        else
            let n=232
        fi
        PS1="\[\033[38;05;${n}m\][\t] \h \w \! \$ \[\033[00m\] " 
    }
    PROMPT_COMMAND=_cmd

Last edited by cryingthug; 10-02-2011 at 08:31 PM. Reason: error
 
  


Reply

Tags
bash



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
bash PS1 debian setting - syntax armandino Linux - General 10 07-19-2015 04:02 PM
bash script ? Change PS1 color on per user basis tommyttt Linux - Software 5 03-01-2010 12:12 AM
Post your BASH prompt [PS1]? introuble General 11 12-27-2006 03:47 PM
PS1 = * in bash, where '*' = parameters BlueSpirit Slackware 5 11-05-2006 08:21 AM
bash PS1 problems Krelian Linux - General 2 10-07-2006 09:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:36 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration