2

I have confusion in the meaning of command ~/

if x is a directory, then what is the meaning of ~/x in bash.

Please guide me

2 Answers 2

2

Unix shells commonly, and bash does, expand ~ to the path to the current user's home directory. This path is often also available as the HOME environment variable, so ~ and $HOME are normally equivalent.

So when you give a path ~/x, you are saying "the path to my home directory, followed by /x". You could also write $HOME/x and get the same result.

If your home directory is at /home/umar, then this expands to /home/umar/x, which is a valid path. If x exists in your home directory, the pointed-at file or directory also exists and can be used (assuming sufficient permissions, which you will normally have to anything in your home directory).

1

~ is a path relative to the user, and represents the logged on users home directory root (or more specically, the path indicated by the logged in user's environment configuration). if I run a standard debian system as the user Bob, then my profile/home exists by default at /home/bob. In this scenario, ~ would evaluate as /home/Bob. ~ is both a convenient shortcut, and also a means to abstract away the root location of the users profile, if it is somewhere unpredictable on the filesystem.

One benefit to using ~, is that you can write scripts that execute the same way for any user regardless of what their username is or whether their profile is stored in the default location. By abstracting away the actual file system paths to the user profile, scripts can be portable across systems that handle profiles differently, or allow a user to move the profile to a non-standard location.

2
  • "the logged on user's home directory" is not necessarily completely accurate. Consider for example the case of logging in, then using sudo -i. One could argue that the logged on user is still the original user (whoever is at the keyboard) whereas the current effective user is that of (almost certainly in this simple case) root.
    – user
    Jun 23, 2015 at 11:33
  • sudo -i is a special hack designed for SU operations specifically. if you run sudo -s, you will observe the default behavior, where ~ switches to /root (at least on debian systems). Sudo tries to preserve the real users environment info, which is not really normal, but necessary in that narrow circumstance. added commend about environment to address your concern. Jun 23, 2015 at 11:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .