Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's 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.
But ~ means "the home directory of the user", and if your distribution has made /root the home directory for the superuser, you're correct. After all you could, if you wanted to, set /var/log (just an example) to be the home directory of user foo, and then when you were logged in as foo and went to your home directory /var/log, your command prompt would show it as ~.
EDIT: the message in the above example was simply that /home/user is by no means the only place where a user can have it's homedirectory. It's up to the system admin to decide where the home directory resides.
To answer your question, as far as I know, bash does not have an escape sequence for what you're looking for. However, you could tailor it. The following is a quick and easy sample, but since it will give you the entire working directory, you might want to work on it to shorten up the return.
root's .bashrc:
Code:
directory=`pwd`
PS1="[\u@\h $directory]\$ "
When logged in as root and within root's home directory, this will output
Code:
[root@host /root]#
(I notice you use >> rather than # to signify root. If you still want to use this, just replace the \$ with >>.)
EDIT: The above isn't correct. See my next response for the explanation...
Quick solution, instead of doing su -, do just an su. This will leave you in current working directory. By doing an su -, it reads the (in this case) roots env, and goes to roots homedir. In that case "~" = /root. If you do a su - user2, than you'll be in /home/user2. An easy redirect would be su - root, then cd ~user. You;ll now be in the /home/user directory.
And with all of the above said: you REALLY need to just change your
idea of what ~ stands for. It's "the effective users home directory"
which can be ANYWHERE in the file-system. All you need to know is that
it's "your home". Work-arounds may be all-right, but never rely on them
because there will come a time when you're at a machine you haven't set-up.
I made a mistake with my previous reply. What I had done will not work, as pwd is only getting evaluated once. This means that after you start the session, the displayed working directory will not change, regardless of where you move to in the filesystem. I tried putting the command directly into the PS1 variable, but that didn't work either. So I'm not entirely sure if you could accomplish that task manually.
However, I'm fairly certain that \W will work (it displays root for me). Are you certain that after making the change, you re-sourced your .bashrc? You can do this with either the . command or source while in your home directory:
Code:
. .bashrc
# or
source .bashrc
That being said, I agree with the advice on changing your concept of ~.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.