Difference between $ HOME and '~' (tilde)?

I always thought that $HOME and ~ were exactly the same and therefore could be used interchangeably. Today, when I tried to install pylibmc, python binding to memcached, on my shared server using ~ gave me an error, but not $HOME . I would like to explain why.

libmemcached is a requirement for pylibmc. I have libmemcached installed under my home directory because I do not have a root on the server. As a result of install pylibmc, I need to make sure that the installation script knows where to find libmemcached.

When executing python setup.py install --with-libmemcached=~ install script works

 gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall \
   -Wstrict-prototypes -fPIC -DUSE_ZLIB -I ~ / include \
   -I / usr / local / include / python2.7 -c _pylibmcmodule.c \
   -o build / temp.linux-i686-2.7 / _pylibmcmodule.o -fno-strict-aliasing

which gives errors that libmemcached could not be found.

If I go to --with-libmemcached=$HOME , the script runs

 gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall \
   -Wstrict-prototypes -fPIC -DUSE_ZLIB -I / home / waterbotte / include \
   -I / usr / local / include / python2.7 -c _pylibmcmodule.c \
   -o build / temp.linux-i686-2.7 / _pylibmcmodule.o -fno-strict-aliasing

no problem. The problem seems to be that the tilde is not resolved. But why?

+57
linux bash centos
Jul 20 '12 at 21:45
source share
5 answers

The shell replaces ~ with the user's home directory ( update : or possibly another user's home directory if ~ something other than / follows), but only if this is the first character of the word.

--with-libmemcached=~ doesn't have ~ not at the beginning, so the shell leaves it alone.

+32
Jul 20 2018-12-12T00:
source share
— -
+42
Jul 20 2018-12-21T00:
source share

~ expands ONLY if it is the first character of the word AND and does not appear

 $ echo "~" ~ $ echo foo~ foo~ $ echo ~ /home/guest $ echo ~/foo /home/guest/foo 

~username expands to HOME username .

 $ echo ~root /root $ echo ~invaliduser ~invaliduser 

To quote file names, you must use $HOME or specify a suffix

 $ echo "$HOME/foo bar" /home/guest/foo bar $ echo ~/"foo bar" /home/guest/foo bar $ echo ~root/"foo bar" /root/foo bar 

Check out the following from the POSIX Tilde Extension

The path name resulting from tilde expansion should be treated as quoted to prevent it from being changed by dividing the field and expanding the path.

+23
Jun 04 '15 at 16:17
source share

The main difference:

 cd /tmp ls "$HOME" #works ls "~" #nope 

So, the shell expands ~ only in a few situations. In your case, python script simple got ~ inside the script - not the output value.

+9
Jul 20 '12 at 22:12
source share

Run the following script:

 #!/bin/bash sudo -H -u root bash<<EOF echo $HOME echo ~ EOF 

Exit:

 /home/my_current_user /root 

You can see that ~ expanded later by the target shell (launched by root ), and $HOME is replaced by the original shell (launched by my_current_user )

0
Jul 15 '19 at 2:37
source share



All Articles