92

I had always thought that $HOME and ~ were exactly the same and thus could be used interchangeably. Today, when I tried to install pylibmc, a python binding to memcached, on my shared server the use of ~ gave me error but not $HOME. I would like to reason out why.

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

When executing python setup.py install --with-libmemcached=~, the installation script runs

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 the errors that libmemcached can't be found.

If I change 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

without any problem. It looks like the problem is that tilde doesn't get resolved. But why?

1
  • 8
    I consider tilde to be a command-line convenience. In scripts, $HOME should always be used instead. Jul 20, 2012 at 21:52

5 Answers 5

60

The tilde is part of a shell expansion (like in bash, csh, zsh, etc). The $HOME variable is exportable and can be used independent of a specific shell.

2
  • 1
    Does it mean that tilde depends on the shell?
    – Porcupine
    Mar 14, 2019 at 9:01
  • @Nikhil technically that's true, but I don't know of any shell that doesn't use the tilde in the same way as all the other shells
    – Jon Lin
    Mar 14, 2019 at 13:22
46

~ is expanded ONLY if it is the first character of a word AND it is unquoted

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

~username is expanded to the HOME of the username.

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

To quote filenames, you should use $HOME or quote the suffix

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

Note the following from "POSIX Tilde Expansion"

The pathname resulting from tilde expansion shall be treated as if quoted to prevent it being altered by field splitting and pathname expansion.

40

The shell replaces ~ with the user's home directory (update: or perhaps by the home directory of some other user, if ~ is followed by something other than a /), but only if it's the first character of a word.

--with-libmemcached=~ has ~ not in the beginning, so the shell leaves it alone.

5
  • 2
    stromberg@aw50 ~ $ echo abc~def abc~def stromberg@aw50 ~ $ echo ~def ~def stromberg@aw50 ~ $ echo def~ def~ stromberg@aw50 ~ $ echo abc${HOME}def abc/home/strombergdef stromberg@aw50 ~ $ echo ${HOME}def /home/strombergdef stromberg@aw50 ~ $ echo def${HOME} def/home/stromberg stromberg@aw50 ~ $ Jul 20, 2012 at 22:00
  • @user1277476: This is very hard to read. Please see the update. Jul 20, 2012 at 22:03
  • Interesting fact to learn. Exactly what I am looking for. Thanks n.m. Jul 20, 2012 at 22:24
  • @user1277476 echo is a built in shell command, the shell knows if you want to echo something, the argument is not referring to a file, since ~ is a built in shell expansion, the shell knows better and won't replace it with your home directory.
    – Jon Lin
    Jul 21, 2012 at 5:34
  • It should be mentioned that there's more to tilde expansion than this. See the bash manual on Tilde Expansion. For instance, tilde is expanded when used in variable assignment (e.g. SOME=~) even though it's not the first character of a word then.
    – user_
    Apr 21, 2020 at 18:38
11

The main difference is:

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

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

4

Run the following script:

#!/bin/bash

sudo -H -u root bash<<EOF
echo $HOME
echo ~
EOF

Output:

/home/my_current_user
/root

You can see that ~ gets expanded later, by the target shell (run by root) while $HOME gets substituted by the source shell (run by my_current_user)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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