Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Maybe not programming related, but I want to reference this directory in c program.

I thought it is $HOME, but when I unset $HOME, I can still cd ~/ in bash.

It is also not $USER's home, since I can be root with USER="another", but the “~/" still point to "/root".

so how bash explains this "~/"?.

share|improve this question

migrated from stackoverflow.com Sep 17 '09 at 9:09

This question came from our site for professional and enthusiast programmers.

    
possible duplicate of What does the little squiggly ~ do in Linux? –  Wuffers Feb 18 '12 at 3:51

5 Answers 5

up vote 2 down vote accepted

Maybe not programming related, but I want to reference this directory in c program.

If you try to opendir the string literal "~/" in C, you'll find it doesn't exist. It is a Bash/Csh shorthand notation that is expanded by the shell to the home directory. It does not exist as such on the filesystem and so a C program will fail- unless it invokes Bash, for example, and allows Bash to expand the string.

share|improve this answer

Using home dir information from /etc/passwd (6th field).

share|improve this answer
4  
No, don't do this. Use (in shell) getent passwd, or in C, getpwent(3). Both will succeed on NIS or LDAP systems where /etc/passwd will fail you. –  kmarsh Oct 2 '09 at 16:01

It is $HOME, but changing it's value will not effect expansion. See this link for more information.

share|improve this answer
    
Changing its value will, but unsetting it won't. When $HOME is unset, bash sets it while expanding ~, as can be checked by running strace echo ~ On Cygwin I see this line: 79 56614 [main] echo 2380 cygheap_user::ontherange: Set HOME (from /etc/passwd) to /cygdrive/g –  reinierpost Sep 17 '09 at 10:04

Bash Hackers Wiki - Documentation - Tilde Expansion:

The tilde expansion is used to expand to several specific pathnames:

  • home directories
  • current working directory
  • previous working directory
share|improve this answer

~ is interpreted as home-dir by the shell. it's a "keyword"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.