1827458 Members
5292 Online
109965 Solutions
New Discussion

OS prompt

 
SOLVED
Go to solution
Tonatiuh
Super Advisor

OS prompt

Default prompt shows:
[username@hostname directory] and an indicator # if root user and $ if another user.

Example:
[oracle@myhostname mydir]$

Using command PS1="..." I can adjust to show more data in the prompt. What variables or parameters or syntax should I use to show the OSUser and the indicator # (for root) or the $ for another users?
8 REPLIES 8
Wim Van den Wyngaert
Honored Contributor
Solution

Re: OS prompt

man bash and search for PS1. After a few matches the list is given.

Wim
Wim
Wim Van den Wyngaert
Honored Contributor

Re: OS prompt

And the answer is
PS1="\u\$"

Wim
Wim
Paul Cross_1
Respected Contributor

Re: OS prompt

This depends on the shell you use.
I'm assuming you use bash, which is the default shell. If so, the prompt commands are all in the man page for that shell under PROMPTING.

For your example, to set the prompt to show the hostname,dash,userid followed by the $ or # do:

PS1="\h-\u\$"

ciao!

Tonatiuh
Super Advisor

Re: OS prompt

Wim, Paul,

The $ symbol, just show the same symbol always, but does not change with root or another user, as the default.
Paul Cross_1
Respected Contributor

Re: OS prompt

You are right. For the # to show up, your effective UID has to be 0. This means you cannot use su.

In root's .bashrc, you might just want to put in:

PS1="\h-\u#"

-p
Stuart Browne
Honored Contributor

Re: OS prompt

Are you using '$' or '\$' at the end of your PS1 string?

'\$' is the inbuilt which gets translated to $ or # for a non-priv/priv user.
One long-haired git at your service...
Stuart Browne
Honored Contributor

Re: OS prompt

Oh, I forgot.. Beware of shell expansion when setting PS1.

If you:

PS1="\u@\h blah\$"

It won't do what's expected (as the \'s will protect the 'u', the 'h' and a '$' symbol).

So always echo $PS1 back after setting it to make sure it's what you expected.

You may need to escape the \'s (\\$ for instance).

Check your /etc/bashrc for examples.
One long-haired git at your service...
Paul Cross_1
Respected Contributor

Re: OS prompt

argh! my answer was totally wrong! I was sleeping at the wheel. su - gives you an effective uid of 0, i confused real uid vs effective uid. sorry.

Stuart is right, you need to escape the \ like this:

PS1="\h-\u\\$"

-p