Linux - GeneralThis 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
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 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.
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.
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
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
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
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).
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).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.