7

Since ~, . and .. are special directories, why are they handled differently in the following example?

$ echo ~
/home/tim
$ echo ..
..
$ echo .
.
  1. ~ is expanded into the dir, but the other two are not.
  2. The other two are expanded literally, but ~ isn't.
Braiam
  • 35,380
  • 25
  • 108
  • 167
Tim
  • 98,580
  • 191
  • 570
  • 977
  • 8
    `~` is not a directory, it is a construct of your shell and does not truly exist. – casey Jul 31 '14 at 16:08
  • `.` is not (the name of) a directory either, but `~` refers to your home directory as `.` refers to the current directory and `..` refers to the parent directory. – iconoclast Jan 16 '15 at 03:02

1 Answers1

18

~ is a special name expanded by the shell, . and .. are real proper directory names, so no expansion is done by the shell there.

Ulrich Schwarz
  • 15,669
  • 4
  • 47
  • 58
  • 4
    For example, type `ls -a` and you'll see the `.` and `..` directories. The only reason they're not usually shown is that anything beginning with `.` is hidden by default. – Useless Jul 31 '14 at 17:30
  • 4
    Another way to see the difference is to quote it: `ls -ld '~'` will give an error (unless there's a file of directory named `~` in the current directory) while `ls -ld '.'` will show information about the current directory. – celtschk Jul 31 '14 at 18:15
  • +1 Might also be useful to mention the fact that `.` is a hard link to the current directory and `..` is a hard link to the current directory's parent. – Joseph R. Aug 24 '14 at 01:51
  • `.` is *not* a directory name, unless you issue `mkdir '.'` – iconoclast Jan 16 '15 at 03:03