Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of tilde in Linux bash (not home directory)

First off, I know that ~/ is the home directory. CDing to ~ or ~/ takes me to the home directory.

However, cd ~X takes me to a special place, where X seems to be anything.

In bash, if I hit "cd ~" and hit tab, it shows a bunch of possible ~X options like ~mail and ~postgres and ~ssh. Going to those folders and doing a pwd shows me that these folders are not in the home directory; they're all over the place.

They are not aliases. I've checked. They're not env. variables, or else they'd require a $.

What is setting these links, and where can I find where these are being set?

like image 609
jbu Avatar asked Jun 15 '09 21:06

jbu


People also ask

What does tilde do in Bash?

Bash also performs tilde expansion on words satisfying the conditions of variable assignments (see Shell Parameters) when they appear as arguments to simple commands. Bash does not do this, except for the declaration commands listed above, when in POSIX mode.

Is tilde a home?

If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

What is the use of tilde symbol (~) in the command line interface of Linux?

Following the colon, you'll see in blue the present working directory. In this case, it shows the tilde ~ symbol, which represents the users home folder. You can use the pwd command to see the full path. Finally, this prompt ends in a dollar sign $ .


2 Answers

It's a Bash feature called "tilde expansion". It's a function of the shell, not the OS. You'll get different behavior with csh, for example.

To answer your question about where the information comes from: your home directory comes from the variable $HOME (no matter what you store there), while other user's homes are retrieved real-time using getpwent(). This function is usually controlled by NSS; so by default values are pulled out of /etc/passwd, though it can be configured to retrieve the information using any source desired, such as NIS, LDAP or an SQL database.

Tilde expansion is more than home directory lookup. Here's a summary:

~              $HOME ~fred          (freds home dir)  ~+             $PWD       (your current working directory) ~-             $OLDPWD    (your previous directory) ~1             `dirs +1` ~2             `dirs +2` ~-1            `dirs -1` 

dirs and ~1, ~-1, etc., are used in conjunction with pushd and popd.

Edited to add:

As Sean Bright pointed out in a comment, the baseline tilde behavior regarding home directories is codified as standard behavior for POSIX-compliant shells. Additionally, the wordexp() C API function is specified to implement this behavior. Though, obviously, use with caution.

like image 111
tylerl Avatar answered Oct 02 '22 20:10

tylerl


Those are the home directories of the users. Try cd ~(your username), for example.

like image 28
Ana Betts Avatar answered Oct 02 '22 18:10

Ana Betts