Linux bash tilde value (not home directory)

Firstly, I know that ~/ is the home directory. CDing up to ~ or ~/ will bring me to my home directory.

However, cd ~X takes me to a special place where X looks like something.

In bash, if I press " cd ~ " and click on the tab, it shows a bunch of possible ~X options like ~mail and ~postgres and ~ssh . Going to these folders and doing pwd shows me that these folders are not in the home directory; they are everywhere.

They are not aliases. I checked. They are not env. variables env. otherwise they will require $ .

What installs these links and where can I find where they are installed?

+82
linux bash home-directory
Jun 15 '09 at 21:58
source share
8 answers

This is a Bash function called tilde extension . This is a shell function, not an OS. For example, you will get different behavior with csh.

To answer the question of where the information is coming from, your home directory comes from the $HOME variable (no matter what you store there), while other user homes are retrieved in real time using getpwent() . This feature is usually controlled by NSS ; therefore, the default values ​​are extracted from /etc/passwd , although it can be configured to extract information using any desired source, such as NIS, LDAP, or SQL database.

The Tilde extension is more than a home directory search. Here is the summary:

 ~ $HOME ~fred (freds home dir) ~+ $PWD (same effect as ./) ~- $OLDPWD (your previous directory) ~1 `dirs +1` ~2 `dirs +2` ~-1 `dirs -1` 

dirs and ~1 , ~-1 etc. used together with pushd and popd .

+54
Nov 02 '09 at 8:41
source share

These are user home directories. For example, try cd ~(your username) .

+32
Jun 15 '09 at 10:01
source share

Are they home directories of users in /etc/passwd ? Services such as postgres, sendmail, apache, etc., create system users who have home directories like regular users.

+16
Jun 15 '09 at 10:00
source share

these are users, check your / etc / passwd

cd ~ username

will bring you to the users home directory

+13
Jun 15 '09 at 22:02
source share

On my machine, due to the fact that I have things, do:

 cd ~ # /work1/jleffler cd ~jleffler # /u/jleffler 

The first draws attention to the value of the $HOME environment variable; I intentionally installed $HOME on the local file system instead of the file system installed on NFS. The second is read from the password file (approximately, NIS complicates the situation a bit) and discovers that the password file says that my home directory is /u/jleffler and changes to this directory.

It is an unfortunate fact that most programs behave as described above (and the POSIX specification for the shell requires this behavior). I use some software (and I don’t have much choice about use) that processes information from the password file as the current value of $ HOME, which is incorrect.

Applying this to the question, as others have pointed out, " cd ~x " goes to the user's home directory "x", and, in general, whenever the tilde extension is performed, ~x means the user's home directory 'x' (and this is an error, if user 'x' does not exist).




It could be noted that:

 cd ~- # Change to previous directory ($OLDPWD) cd ~+ # Change to current directory ($PWD) 

I cannot immediately find a use for ' ~+ ' unless you are doing some strange stuff with moving symbolic links in the path leading to the current directory.

You can also do:

 cd - 

This means ~- .

+9
Jun 15 '09 at 22:37
source share
+5
Jun 15 '09 at 23:03
source share

If you use autofs , then the extension can really come from /etc/auto.home (or /etc/auto.home similar for your distribution). For example, my /etc/auto.master looks like this:

 /home2 auto.home --timeout 60 

and /etc/auto.home as follows:

 mgalgs -rw,noquota,intr space:/space/mgalgs 
0
Nov 21 '14 at 18:43
source share

You may see that OpenDirectory / ActiveDirectory / LDAP users are "automated" in your home directory.

In * nix, ~ your home directory will be resolved. Similarly ~X will be allowed to "user X".

Like automount for directories, OpenDirectory / ActiveDirectory / LDAP is used in larger / enterprise environments to automate user directories. These users may be actual people or they may be computer accounts created to provide various functions.

If you type ~ Tab , you will see a list of users on your computer.

0
Apr 27 '16 at 18:06
source share



All Articles