How can I set the '~' in my shell

0

Normally, ~ in my shell maps to /home/myuserid. How can I make it map to another directory?

michael

Posted 2012-11-22T23:52:49.433

Reputation: 4 127

6Okay, I really, really have to know why you want this. – Ignacio Vazquez-Abrams – 2012-11-22T23:55:59.703

3Please elaborate a bit more on the context. You should be able to solve your problem another way. – gertvdijk – 2012-11-23T00:16:06.603

Answers

5

If you really want to do this, you can export HOME=/directory/you/want.

E.g.

~ $ export HOME=/tmp
/home/youruser $ cd ~
~ $ pwd
/tmp

But do note that this can break stuff, as noted in a comment: anything that refers to your home directory via $HOME or ~ will be broken.

Renan

Posted 2012-11-22T23:52:49.433

Reputation: 7 463

3But expect things to start breaking left and right in any of the subprocesses. – Ignacio Vazquez-Abrams – 2012-11-22T23:56:37.130

1

You can also assign a different path in /home/user/.bashrc for HOME environment variable.
export HOME=newpath Eg. export HOME=/home/newuser This will make the change permanent for every session and ~ will always point to another specified directory.

Manoj Agarwal

Posted 2012-11-22T23:52:49.433

Reputation: 203

0

Different options for different circumstances:

  1. On both Linux and Cygwin, you can set the home directory temporarily by setting the HOME environment variable. Use the following command to do this (note the lack of spaces around the =)

    export HOME=/new/dir/path
    

    This is only a temporary change, and not every program will pick up on it. The setting will change if you start a new session or reboot.

  2. On Linux (at least Red Hat, and I'd be very surprised if Ubuntu differs), you can make the change permanent by running the following command (you may need to be root and/or prefix the command with sudo).

    usermod -d /new/dir/path username
    

    This is preferable to Manoj's answer, since that solution is effectively just repeating option 1 every time you open a Bash shell; not every program will pick up on the new directory.

    For this one to take effect, you'll need to restart all your sessions, or just reboot your computer.

  3. On Cygwin, for a permanent change you need to edit Cygwin's /etc/passwd file in your choice of text editor. (To do it using a Windows editor, it'll probably be in C:\cygwin\etc\passwd, but make sure you use an editor which preserves line endings, so not Notepad.)

    That file will have a line that looks something like the below:

    username:unused:23456:12345:U-GRP\username,S-1-2-34-567890-123456-789012:/old/home/dir:/bin/bash
    

    You need to change the bits between the last and second-to-last :s (which reads /old/home/dir in the above) to specify the directory you want as the user's home directory.

    Again, for this to take effect properly, you'll need to close all your Cygwin processes and reopen them, or just restart your computer to be sure of doing it properly.

Caution: Cygwin does not get on with having spaces in the home directory name. It's not supported, causes untold trouble, and the consistent response on the Cygwin mailing list when people have trouble is "well don't do that then". In particular, this means a home directory like /cygwin/c/Documents and Settings/username/My Documents is a Bad Idea™.

me_and

Posted 2012-11-22T23:52:49.433

Reputation: 2 118