LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 05-22-2008, 02:30 PM   #1
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Rep: Reputation: 164Reputation: 164
\$ in Bash's PS1 not working for root


When I set the simple command PS1="\$" in /etc/bashrc, my root prompt is "$", not "#". I have never been able to get it to work properly. Best I can think of is that bash sees it as an escaped $, rather than the prompt symbol.

Any thoughts?
 
Old 05-22-2008, 04:15 PM   #2
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
I'm not exactly clear here. Do you want the root user to have a '#' prompt and other users to have a '$'?

/etc/bashrc is the system-wide bash settings file. If you simply change the prompt there, then of course you're going to see it show up in all consoles, including root.

You have two choices. First, you can remove the line from /etc/bashrc and place it instead in the individual ~/.bashrc files of only the users you want to change. (Note that root also has it's own 'home' folder, /root, and its own .bashrc.

Your second choice is to use an 'if' statement to detect when the user is root. Something like this:

Code:
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
 
Old 05-22-2008, 05:03 PM   #3
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Quote:
Originally Posted by David the H. View Post
I'm not exactly clear here. Do you want the root user to have a '#' prompt and other users to have a '$'?
Yes, that's correct. This is standard bash prompt ideology, the use of a "#" for root, as opposed to "$" for normal users.

Quote:
Originally Posted by David the H. View Post
/etc/bashrc is the system-wide bash settings file. If you simply change the prompt there, then of course you're going to see it show up in all consoles, including root.

You have two choices. First, you can remove the line from /etc/bashrc and place it instead in the individual ~/.bashrc files of only the users you want to change. (Note that root also has it's own 'home' folder, /root, and its own .bashrc.
It's my understanding that each time a user logs in, the /etc/bashrc is executed, then the local user's .bashrc is read. Therefore, PS1 in ~/.bashrc will take precidence over PS1 in /etc/bashrc. With that, even when I place 'PS1="\$"' in root's .bashrc, it still shows '$ '.

In the PROMPTING section of the bash man page, \$ is described as having the following function:
Code:
\$     if the effective UID is 0, a #, otherwise a $
If this is different from the function I think it is, please clarify this.

Quote:
Originally Posted by David the H. View Post
Your second choice is to use an 'if' statement to detect when the user is root. Something like this:

Code:
    if [ "`id -u`" -eq 0 ]; then
      PS1='# '
    else
      PS1='$ '
    fi
I have a function similar to yours that does what I want. But this is not optimal, and for learning purposes I would like to know why a simple 'PS1="\$"' doesn't display "#" for root.
 
Old 05-28-2008, 12:06 PM   #4
SlowCoder
Senior Member
 
Registered: Oct 2004
Location: Southeast, U.S.A.
Distribution: Debian based
Posts: 1,250

Original Poster
Rep: Reputation: 164Reputation: 164
Pffff! No resolution on this one? Darn.
 
  


Reply



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
Python equivalent to bash's $* wapcaplet Programming 2 10-30-2005 12:30 AM
Changing PS1 "on the fly" for root gandalf2041 Linux - General 1 11-22-2003 01:37 PM
Bash's "present working directory" doesn't exit jonr Linux - General 2 11-07-2003 01:58 PM
bash's source code ramu_ak Linux - General 2 02-16-2003 06:06 PM
bash's source code ramu_ak Linux - General 0 02-16-2003 08:32 AM

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

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