1

I was rebuilding the bash command ls using C as a unix programming book exercise and had a look at various dir functions. And I realized, that chdir, opendir and similar don't accept pathnames starting with ~ but do accept . or ..
First, why? And second, is the source code of the real unix ls command somewhere visible? Because ls ~ does indeed work and I am curious how this works.

Lavair
  • 888
  • 10
  • 21

2 Answers2

2

~ is a shell shortcut that expands to the home directory set in /etc/passwd or ldap configuration. The kernel has no concept of home directory and has no idea what ~ directory is.

Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
0

The ~ is expanded by the shell into the home directory.

You can look up the shell's source code to find the expansion.

S.S. Anne
  • 15,171
  • 8
  • 38
  • 76