You are not logged in.

#1 2009-12-19 04:05:53

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Why is my root prompt a $?

I use \$ and got a $.

Last edited by Anikom15 (2009-12-19 04:06:17)


Personally, I'd rather be back in Hobbiton.

Offline

#2 2009-12-19 04:16:43

some-guy94
Member
Registered: 2009-08-15
Posts: 360

Re: Why is my root prompt a $?

Use \\$

Offline

#3 2009-12-19 04:28:41

Anikom15
Banned
From: United States
Registered: 2009-04-30
Posts: 836
Website

Re: Why is my root prompt a $?

That worked, but why?


Personally, I'd rather be back in Hobbiton.

Offline

#4 2009-12-19 05:01:07

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Why is my root prompt a $?

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.

Offline

#5 2009-12-19 08:05:38

Aprz
Member
From: Newark
Registered: 2008-05-28
Posts: 277

Re: Why is my root prompt a $?

Works for me just using single quotes.

PS1='[\u@\h \W]\$ '

Last edited by Aprz (2009-12-19 08:14:43)

Offline

#6 2009-12-19 09:34:04

lolilolicon
Member
Registered: 2009-03-05
Posts: 1,722

Re: Why is my root prompt a $?

skottish wrote:
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:

man bash wrote:

\$     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

Board footer

Powered by FluxBB