LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 06-27-2020, 10:21 PM   #1
El Rengo
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Rep: Reputation: 0
Question Doubt about PS1


Hi! I've a doubt with PS1 prompt customization. I've my own PS1 config on .bashrc file. I shared:

Code:
####################################################################################################
# List of Colors
##########################################################################################################

# Reset
Color_Off='\e[0m'       # Text Reset

# Regular Colors
Black='\e[0;30m'        # Black
Red='\e[0;31m'          # Red
Green='\e[0;32m'        # Green
Yellow='\e[0;33m'       # Yellow
Blue='\e[0;34m'         # Blue
Purple='\e[0;35m'       # Purple
Cyan='\e[0;36m'         # Cyan
White='\e[0;37m'        # White

# Bold
BBlack='\e[1;30m'       # Black
BRed='\e[1;31m'         # Red
BGreen='\e[1;32m'       # Green
BYellow='\e[1;33m'      # Yellow
BBlue='\e[1;34m'        # Blue
BPurple='\e[1;35m'      # Purple
BCyan='\e[1;36m'        # Cyan
BWhite='\e[1;37m'       # White

# Underline
UBlack='\e[4;30m'       # Black
URed='\e[4;31m'         # Red
UGreen='\e[4;32m'       # Green
UYellow='\e[4;33m'      # Yellow
UBlue='\e[4;34m'        # Blue
UPurple='\e[4;35m'      # Purple
UCyan='\e[4;36m'        # Cyan
UWhite='\e[4;37m'       # White

# Background
On_Black='\e[40m'       # Black
On_Red='\e[41m'         # Red
On_Green='\e[42m'       # Green
On_Yellow='\e[43m'      # Yellow
On_Blue='\e[44m'        # Blue
On_Purple='\e[45m'      # Purple
On_Cyan='\e[46m'        # Cyan
On_White='\e[47m'       # White

# High Intensity
IBlack='\e[0;90m'       # Black
IRed='\e[0;91m'         # Red
IGreen='\e[0;92m'       # Green
IYellow='\e[0;93m'      # Yellow
IBlue='\e[0;94m'        # Blue
IPurple='\e[0;95m'      # Purple
ICyan='\e[0;96m'        # Cyan
IWhite='\e[0;97m'       # White

# Bold High Intensity
BIBlack='\e[1;90m'      # Black
BIRed='\e[1;91m'        # Red
BIGreen='\e[1;92m'      # Green
BIYellow='\e[1;93m'     # Yellow
BIBlue='\e[1;94m'       # Blue
BIPurple='\e[1;95m'     # Purple
BICyan='\e[1;96m'       # Cyan
BIWhite='\e[1;97m'      # White

# High Intensity backgrounds
On_IBlack='\e[0;100m'   # Black
On_IRed='\e[0;101m'     # Red
On_IGreen='\e[0;102m'   # Green
On_IYellow='\e[0;103m'  # Yellow
On_IBlue='\e[0;104m'    # Blue
On_IPurple='\e[0;105m'  # Purple
On_ICyan='\e[0;106m'    # Cyan
On_IWhite='\e[0;107m'   # White

##########################################################################################################
# Custom prompt
##########################################################################################################

PS1="[\[$BGreen\]\w\[$Color_Off\]]\n[\[$BIRed\]\u\[$BWhite\]@\[$BIBlue\]\h\[$Color_Off\]]\\$ "
I do not undertand why for "\w" or "\u" or "\h" I do not need double "" but for "\$" it's required "\\$"
Thanks!

elrengo!

Last edited by El Rengo; 06-28-2020 at 10:57 AM. Reason: SOLVED
 
Old 06-28-2020, 04:49 AM   #2
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 14,976
Blog Entries: 9

Rep: Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240Reputation: 4240
Quote:
Originally Posted by El Rengo View Post
I do not undertand why for "\w" or "\u" or "\h" I do not need double "" but for "\$" it's required "\\$"
One is an actual $ sign, the other changes depending on which user you are:
Code:
#PS1="\$"
$PS1="\\$"
#PS1="\$"
$whoami
root
$PS1="\$"
$PS1="\\$"
#exit
logout
$whoami
ondoho
$PS1="\$"
$PS1="\\$"
$whoami
ondoho
 
Old 06-28-2020, 05:02 AM   #3
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,875

Rep: Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890
Quote:
Originally Posted by El Rengo View Post

I do not undertand why for "\w" or "\u" or "\h" I do not need double "" but for "\$" it's required "\\$"
You need to think a bit differently: \ and the following char together (as an expression) has a special meaning.
\w, \u, \h or \\.
Additionally it is evaluated once when you set PS1 and second time when it printed. \w and \u should be evaluated that time (when printed), but $ at the end should be taken as is.
So \\ will be \ at the first step and \$ will be $ at the second.
 
1 members found this post helpful.
Old 06-28-2020, 10:55 AM   #4
El Rengo
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by ondoho View Post
One is an actual $ sign, the other changes depending on which user you are:
Code:
#PS1="\$"
$PS1="\\$"
#PS1="\$"
$whoami
root
$PS1="\$"
$PS1="\\$"
#exit
logout
$whoami
ondoho
$PS1="\$"
$PS1="\\$"
$whoami
ondoho
Thanks! I know that and for that I would like to use "\$". Because I use the same .bashrc file for root and my user. Using "\$" allow to me use the same .bashrc and know if i have a UI=0 or not.

Quote:
Originally Posted by pan64 View Post
You need to think a bit differently: \ and the following char together (as an expression) has a special meaning.
\w, \u, \h or \\.
Additionally it is evaluated once when you set PS1 and second time when it printed. \w and \u should be evaluated that time (when printed), but $ at the end should be taken as is.
So \\ will be \ at the first step and \$ will be $ at the second.
Thanks! I believe all "\character" have the same behavior.
 
Old 06-28-2020, 04:59 PM   #5
GazL
LQ Guru
 
Registered: May 2008
Distribution: CRUX
Posts: 5,519
Blog Entries: 14

Rep: Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345
Quote:
Originally Posted by El Rengo View Post
I do not undertand why for "\w" or "\u" or "\h" I do not need double "" but for "\$" it's required "\\$"
The rules for double-quotes are a little involved (and have historically differed between shell implementations).

For bash, within double-quotes, '\' only keeps its special meaning (i.e. escape the next character) when it proceeds one of: $ ` \ "
In all other cases it is treated literally.

So
word="\\u" and word="\u" will both set $word to be '\u'
but
word="\\$" and word="\$" will set $word to '\$' and '$' respectively.


Sometimes you're just better off not using double-quotes at all. It may make the assignment a little longer because you might need a few extra '\' in order to get the desired value, but at least '\' acts consistently outside of double quotes, no matter what it proceeds.
 
Old 06-28-2020, 06:55 PM   #6
El Rengo
LQ Newbie
 
Registered: Dec 2008
Posts: 4

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by GazL View Post
The rules for double-quotes are a little involved (and have historically differed between shell implementations).

For bash, within double-quotes, '\' only keeps its special meaning (i.e. escape the next character) when it proceeds one of: $ ` \ "
In all other cases it is treated literally.

So
word="\\u" and word="\u" will both set $word to be '\u'
but
word="\\$" and word="\$" will set $word to '\$' and '$' respectively.


Sometimes you're just better off not using double-quotes at all. It may make the assignment a little longer because you might need a few extra '\' in order to get the desired value, but at least '\' acts consistently outside of double quotes, no matter what it proceeds.
Thanks! But if I use a "'":

Code:
PS1='[\[$BGreen\]\w\[$Color_Off\]]\n[\[$BIRed\]\u\[$BWhite\]@\[$BIBlue\]\h\[$Color_Off\]]\$ '
the result is:

Code:
Last login: Sun Jun 28 20:42:55 2020 from X.X.X.X
[\e[1;32m~\e[0m]
[\e[1;91melrengo\e[1;37m@\e[1;94mlab-oss1\e[0m]$
 
Old 06-29-2020, 02:13 AM   #7
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 14,875

Rep: Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890Reputation: 4890
because evaluation works differently with ' and ". Actually ' disables almost everything.
https://stackoverflow.com/questions/...quotes-in-bash
 
Old 06-29-2020, 03:02 AM   #8
GazL
LQ Guru
 
Registered: May 2008
Distribution: CRUX
Posts: 5,519
Blog Entries: 14

Rep: Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345Reputation: 3345
My suggestion was to not use any quotes when setting PSn

Here's mine, as example:
Code:
# ANSI Terminal control sequences to implement text styles,
# for use in prompts (and/or elsewhere):
declare -A style=(
  [default]=$( tput -S <<< $'sgr0\nop' )
  [prompt]=$( tput -S <<< $'sgr0\nop\nbold' )
  [input]=$( tput -S <<< $'sgr0\nop\nbold\nsetaf 5' )
  [warning]=$( tput -S <<< $'sgr0\nop\nbold\nsetaf 3' )
  [error]=$( tput -S <<< $'sgr0\nop\nbold\nsetaf 1' )
)

# Bash prompts:
#   'trap' ensures we clean up after ourselves on exit.
trap "tput -S <<< $'sgr0\nop'" EXIT
PS1=\\[${style[prompt]}\\]\\$\ \\[${style[input]}\\]
PS2=\\[${style[prompt]}\\]\>\ \\[${style[input]}\\]
PS0=${style[default]}
As I said above, you get a few extra '\' along the way, but the rules remain consistent.
 
  


Reply

Tags
linux, prompt, ps1


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Snes, Nes, PS1, SGen Emulators for linux? wh33t Linux - Games 15 11-22-2004 05:08 AM
Changing PS1 "on the fly" for root gandalf2041 Linux - General 1 11-22-2003 01:37 PM
Setting PS1 variable for KDE Konsole shell (RH9) robertmarkbram Linux - Software 4 10-14-2003 07:38 PM
is there a file to configure PS1 and PS2 permanently? kiki Linux - Newbie 3 08-06-2003 10:31 PM
setting the PS1 enviroment variable in xterm qanopus Linux - General 2 04-29-2003 08:41 AM

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

All times are GMT -5. The time now is 11:34 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