LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 07-29-2015, 04:47 AM   #1
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311
Blog Entries: 2

Rep: Reputation: 16
Question bash prompt: Can't change PS3


I have copied the following shell script from my current bash textbook to make ~/bin/ps3.sh:

Code:
#/bin/bash

select i in mon tue wed exit
do
	case $i in
		mon) echo "Monday";;
		tue) echo "Tuesday";;
		wed) echo "Wednesday";;
		exit) exit;;
	esac
done
but when prompted for my selection, I only get the system default "#?". I want the prompt to be "Selection:" written in yellow, and then when I type in my answer the answer's color is again default. Even though I can successfully implement the export command to produce the prompt I want for PS1 and PS2, still PS3 simply doesn't work.
Code:
a@NP-NC110:~$ export PS3="\[\033[01;33m\]Selection: \[033[0m\]"
a@NP-NC110:~$ ps3.sh 
1) mon
2) tue
3) wed
4) exit
\[\033[01;33m\]Selection: \[033[0m\]
Even when I write
Code:
    PS3="\[\033[01;33m\]Selection: \[\033[0m\]"
in ~/.bashrc I still get the same prompt:
Code:
\[\033[01;33m\]Selection: \[033[0m\]
How can I get the prompt to be just the one word "Selection:" written in yellow w/o all the code surrounding it?

Last edited by andrew.comly; 07-29-2015 at 06:13 AM. Reason: title clarity
 
Old 07-29-2015, 05:08 AM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Give this one a try:

Code:
export PS3=$'\e[01;33mSelection: \e[0m'
\e is the same as \033

No idea why you have to use it that way for PS3, $ causes a string to be treated as an ANSI C string. See http://wiki.bash-hackers.org/syntax/quoting.
 
1 members found this post helpful.
Old 07-29-2015, 06:01 AM   #3
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Smile [Solved]

Quote:
Originally Posted by goumba View Post
Give this one a try:

Code:
export PS3=$'\e[01;33mSelection: \e[0m'
Awesome! It works. Strange how even though PS1 and PS2 follow one syntax, PS3 all of a sudden just follows another.

THANKS!!

Last edited by andrew.comly; 07-29-2015 at 06:12 AM.
 
Old 07-29-2015, 08:14 AM   #4
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Talking "ANSI C like strings" lesson complete

Quote:
Originally Posted by goumba View Post
$ causes a string to be treated as an ANSI C string. See http://wiki.bash-hackers.org/syntax/quoting.
I've just completed the above tutorial. It was excellent, I've learned a lot. This syntax is far more robust than the regular type and "ANSI C like strings" syntax will be most useful in my future practice programs.

Last edited by andrew.comly; 07-29-2015 at 08:17 AM.
 
Old 07-29-2015, 01:12 PM   #5
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Great! Isn't that what you came here, was to learn a bit? Glad it worked out for you on both fronts.
 
1 members found this post helpful.
Old 07-29-2015, 10:09 PM   #6
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Exclamation broken again

Quote:
Originally Posted by goumba View Post
Give this one a try:

Code:
export PS3=$'\e[01;33mSelection: \e[0m'
\e is the same as \033
Last night this worked. This morning I tried to use this $'\e method to simplify my ~/.bashrc configuration file. Now it doesn't work anymore, even when I change .bashrc back to it's original state. Currently my ~/.bashrc config file is:
Code:
RS="\[\033[0m\]"                    # reset
HC="\[\033[1m\]"                   # hicolor
FRED="\[\033[01;31m\]"      # foreground red
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;34m\]\h\[\033[01;33m\]:\[\033[01;35m\]\w\[\033[01;32m\]\$ $RS"
    PS2="\[\033[1m\]\[\033[01;34m\]>>\[\033[0m\]"
#\[\033[1m\] 
#PS2="$HC$FRED> $RS"
    PS3=$'\e[01;33mSelection: \e[0m'
else
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;32m\]:\[\033[01;36m\]\w\[\033[01;33m\]\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
Code:
a@NP-NC110:~$ ps3.sh 
1) mon
2) tue
3) wed
4) exit
#?
Still the same "#?" prompt! I even rebooted, still the same thing.

Maybe it's because this $'\e technique only works during certain hours or days?

Last edited by andrew.comly; 07-29-2015 at 10:18 PM.
 
Old 07-29-2015, 10:25 PM   #7
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
Quote:
Originally Posted by andrew.comly View Post
Still the same "#?" prompt! I even rebooted, still the same thing.

Maybe it's because this $'\e technique only works during certain hours or days?
LOL. No.

Code:
PS3=$'\e[01;33m\]Selection: \e[0m\]'
Try that. You were missing the closing brackets.
 
1 members found this post helpful.
Old 07-29-2015, 11:02 PM   #8
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Thumbs down no...

Quote:
Originally Posted by goumba View Post
Code:
PS3=$'\e[01;33m\]Selection: \e[0m\]'
Try that. You were missing the closing brackets.
Doesn't work, specifically I change ~/.bashrc to:
Code:
PS3=$'\e[01;33m\]Selection: \e[0m\]'
and I still get:
Code:
a@NP-NC110:~$ ps3.sh 
1) mon
2) tue
3) wed
4) exit
#?
 
Old 07-29-2015, 11:32 PM   #9
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
I just tried using your prompt, it works for me. Do you have PS3 defined anywhere else in .bashrc or .profile?

My apologies, an error in my last post, closing brackets are not needed.

Edit: Have you set color_prompt to "yes"? If color_prompt is not set to "yes" (usually by setting force_color_prompt to yes in .bashrc), then your PS3 assignment is being ignored. Certain terminal types will cause color_prompt to be set automatically by .bashrc, but in my experience it rarely happens and must be forced. If you don't want to force, can't force, then add a plain version, like so:

Code:
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;34m\]\h\[\033[01;33m\]:\[\033[01;35m\]\w\[\033[01;32m\]\$ $RS"
    PS2="\[\033[1m\]\[\033[01;34m\]>>\[\033[0m\]"
#\[\033[1m\] 
#PS2="$HC$FRED> $RS"
    PS3=$'\e[01;33mSelection: \e[0m'
else
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;32m\]:\[\033[01;36m\]\w\[\033[01;33m\]\$ '
    PS3=$'Selection: '
fi

Last edited by goumba; 07-30-2015 at 12:15 AM.
 
1 members found this post helpful.
Old 07-30-2015, 12:02 AM   #10
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Question force_color_prompt

Quote:
Originally Posted by goumba View Post
Edit: Have you set color_prompt to "yes"? If color_prompt is not set to "yes" (usually by setting force_color_prompt to yes in .bashrc), then your PS3 assignment is being ignored.
My .bashrc is like follows - line 46-57:
Code:
	force_color_prompt=yes

if [ -n "$force_color_prompt" ]; then
    if [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
	# We have color support; assume it's compliant with Ecma-48
	# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
	# a case would tend to support setf rather than setaf.)
	color_prompt=yes
    else
	color_prompt=no
    fi
fi
and lines 80-98:
Code:
if [ "$color_prompt" = yes ]; then
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;34m\]\h\[\033[01;33m\]:\[\033[01;35m\]\w\[\033[01;32m\]\$ $RS"
    PS2="\[\033[1m\]\[\033[01;34m\]>>\[\033[0m\]"
#\[\033[1m\] 
#PS2="$HC$FRED> $RS"
    PS3=$'\e[01;33m\]Selection: \e[0m\]'
else
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;32m\]:\[\033[01;36m\]\w\[\033[01;33m\]\$ '
fi
unset color_prompt force_color_prompt

# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
    PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"
    ;;
*)
    ;;
esac
 
Old 07-30-2015, 12:09 AM   #11
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
It's hard to say, unless for some reason color_prompt=yes is being ignored.

The odd thing is you have a prompt (PS1) with escape color codes that is being used when color_prompt is set to no, which would make it difficult to see which branch is being executed on your machine, because either way you're setting PS1 to include color codes (which is wrong, as if the terminal doesn't support colors it will print the codes and look like a lot of garbage).

It should look like:

Code:
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS3=$'\e[01;33mSelection: \e[0m'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    PS3='Selection: '
fi
Now, if you get an uncolored "Selection:" we'll know color_prompt is being ignored and can investigate that further.

Edit: Another option, I don't know if you're aware, is to set PS3 in the script itself, if you don't want that prompt for every time select is used.

Code:
#/bin/bash
PS3=$'\[\e33;1mSelection: \[\e0m'

select i in mon tue wed exit
do
	case $i in
		mon) echo "Monday";;
		tue) echo "Tuesday";;
		wed) echo "Wednesday";;
		exit) exit;;
	esac
done
When the script is completed, PS3 returns to the default (unless you export PS3).

Last edited by goumba; 07-30-2015 at 12:14 AM.
 
1 members found this post helpful.
Old 07-30-2015, 12:20 AM   #12
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Question PS3 instances

the line 85:
Code:
PS3=$'\e[01;33m\]Selection: \e[0m\]'
is the only instance of "PS3"
Attached Files
File Type: txt bashrc.txt (5.2 KB, 19 views)
 
Old 07-30-2015, 12:38 AM   #13
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Question weird

Quote:
Originally Posted by goumba View Post
It's hard to say, unless for some reason color_prompt=yes is being ignored.

The odd thing is you have a prompt (PS1) with escape color codes that is being used when color_prompt is set to no, which would make it difficult to see which branch is being executed on your machine, because either way you're setting PS1 to include color codes (which is wrong, as if the terminal doesn't support colors it will print the codes and look like a lot of garbage).
I too, found the section:
Code:
    PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u\[\033[01;33m\]@\[\033[01;34m\]\h\[\033[01;33m\]:\[\033[01;35m\]\w\[\033[01;32m\]\$ $RS"
    PS2="\[\033[1m\]\[\033[01;34m\]>>\[\033[0m\]"
    PS3=$'\e[01;33m\]Selection: \e[0m\]'
else
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[01;32m\]:\[\033[01;36m\]\w\[\033[01;33m\]\$ '
especially:
Code:
PS1='${debian_chroot:+($debian_chroot)}
really weird. Being that I didn't add this, I guess this is just the default .bashrc in Lubuntu 14.04.

I wonder what ${debian_chroot:+($debian_chroot)} means?
 
Old 07-30-2015, 12:44 AM   #14
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Fedora, OpenSUSE, FreeBSD, OpenBSD, macOS (hack). Past: Debian, Arch, RedHat (pre-RHEL).
Posts: 1,335
Blog Entries: 7

Rep: Reputation: 402Reputation: 402Reputation: 402Reputation: 402Reputation: 402
I understood that much.

Now, if I add export, the prompt works:

Code:
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    export PS3=$'\e[01;33mSelection: \e[0m'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    export PS3='Selection: '
fi
 
1 members found this post helpful.
Old 07-30-2015, 12:49 AM   #15
andrew.comly
Member
 
Registered: Dec 2012
Distribution: Trisquel-Mini 7.0, Lubuntu 14.04, Debian lxde 8.0
Posts: 311

Original Poster
Blog Entries: 2

Rep: Reputation: 16
Question still same

Quote:
Originally Posted by goumba View Post
It should look like:
Code:
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS3=$'\e[01;33mSelection: \e[0m'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    PS3='Selection: '
fi
Now, if you get an uncolored "Selection:" we'll know color_prompt is being ignored and can investigate that further.
I changed my ~/.bashrc config file to:
Code:
if [ "$color_prompt" = yes ]; then
    PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
    PS2="\[\033[1m\]\[\033[01;34m\]>>\[\033[0m\]"
    PS3=$'\e[01;33mSelection: \e[0m'
else
    PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ '
    PS3='Selection: '
fi
and I still get:
Code:
a@NP-NC110:~$ ps3.sh 
1) mon
2) tue
3) wed
4) exit
#?
I guess I really am quite talentless. Nevertheless, I must keep trying.
 
  


Reply

Tags
bash, prompt



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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Make Bash prompt look like Windows C:\ prompt -- *PROBLEM* arkadios Linux - General 5 07-24-2012 11:29 AM
[SOLVED] Bash prompt. It looks fine initially, but there seems to be a hidden error. WyattOil Linux - General 2 06-01-2011 06:55 PM
Newbie with YDL on PS3 coming for help with Kboot prompt jekylnhyde Linux - Newbie 3 03-07-2010 07:37 AM
failed PS3 linux install, can i rescue my PS3 kells Linux - Hardware 3 01-05-2010 11:33 AM
Cannot get past install at kboot prompt - YDL 6.0 on PS3 valentino46 Linux - Newbie 7 08-22-2008 06:25 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:17 PM.

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