You are not logged in.
Pages: 1
I use \$ and got a $.
Last edited by Anikom15 (2009-12-19 04:06:17)
Personally, I'd rather be back in Hobbiton.
Offline
Use \\$
Offline
That worked, but why?
Personally, I'd rather be back in Hobbiton.
Offline
That worked, but why?
You have to escape the \ because it's a reserved character. It just so happens that the escape character is \. It's like this in string handling in many languages, including the various shells.
Offline
Works for me just using single quotes.
PS1='[\u@\h \W]\$ '
Last edited by Aprz (2009-12-19 08:14:43)
Offline
Anikom15 wrote:That worked, but why?
You have to escape the \ because it's a reserved character. It just so happens that the escape character is \. It's like this in string handling in many languages, including the various shells.
True. It's not just that though.
> echo "\\w\\\$"
\w\$
> echo "\\w\\$"
\w\$
> echo "\\w\\$baz"
\w\
> echo "\\w\\\$baz"
\w\$baz
> echo "\w\$"
\w$
> echo $
$
> echo $baz
The above code illustrates:
1. It's good practice to always escape \ itself as \\ . But you don't have to escape \ , as long as the character following \ combined with \ doesn't form an escape sequence.
2. "\$" forms an escape sequence, just like "\t", "\n", etc. And it means a $ character which has no special meaning.
3. $ itself is a very special character in shell, so it is good practice to always escape it by prepending a backslash, but it doesn't have to be escaped as long as the thing following it combined with this $ does not form something(normally a shell variable)
Let's say, if you put:
PS1='\$\w' # sets PS1 to \$\w #good
PS1="\\\$\\w" # sets PS1 to \$\w #good
PS1="\\$\\w" # sets PS1 to \$\w #works, but is not good practice
PS1="\$\w" # sets PS1 to $\w #you should understand it very well now
What happens when bash expand this PS1? It's first (at assignment) expanded using the normal bash expanding, then the expanded result is stored in $PS1 variable, and this $PS1 is expanded using the special "PROMPT expanding" every time before the PROMPT is displayed.
The "PROMPT expanding" is explained in the PROMPTING section of `man bash'. For example:
\$ if the effective UID is 0, a #, otherwise a $
Which is what Anikom15 wanted to make use of I guess.
Last edited by lolilolicon (2009-12-19 09:51:21)
This silver ladybug at line 28...
Offline
Pages: 1