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