bash $HOME 和'~'(波浪号)之间的区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11587343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 02:50:09  来源:igfitidea点击:

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

linuxbashcentos

提问by tamakisquare

I had always thought that $HOMEand ~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.

我一直认为$HOME~完全相同,因此可以互换使用。今天,当我尝试在我的共享服务器上安装pylibmc(一个绑定到 memcached 的 python 绑定)时,使用~给了我错误,但没有 $HOME. 我想说明原因。

libmemcachedis a requirement for pylibmc. I have libmemcachedinstalled 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.

libmemcachedpylibmc的要求。我在我的主目录下安装了libmemcached,因为我在服务器上没有 root。因此,要安装pylibmc,我需要确保安装脚本知道在哪里可以找到libmemcached

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

执行时python setup.py install --with-libmemcached=~,安装脚本运行

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 libmemcachedcan't be found.

这给出了找不到libmemcached的错误。

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

如果我更改为--with-libmemcached=$HOME,脚本将运行

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?

没有任何问题。看起来问题是波浪号没有得到解决。但为什么?

采纳答案by n. 'pronouns' m.

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.

shell 替换~为用户的主目录(update: 或者可能由其他用户的主目录替换,如果~后跟 a 以外的内容/),但前提是它是单词的第一个字符。

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

--with-libmemcached=~~一开始还没有,所以外壳不理会它。

回答by Jon Lin

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

波浪号是shell 扩展的一部分(如在 bash、csh、zsh 等中)。该$HOME变量是可导出的,可以独立于特定的 shell 使用。

回答by go2null

~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

~usernameis expanded to the HOMEof the username.

~username扩展到HOMEusername

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

To quote filenames, you should use $HOMEor quote the suffix

要引用文件名,您应该使用$HOME或引用后缀

$ 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"

请注意“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.

由波浪号扩展产生的路径名应被视为被引用以防止它被字段拆分和路径名扩展改变。

回答by jm666

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.

所以,shell 只在少数情况下扩展 ~ 。在您的情况下,python 脚本简单地将 ~ 放入脚本中 - 而不是扩展值。

回答by Rufus

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 expandedlater, by the target shell (run by root) while $HOMEgets substitutedby the source shell (run by my_current_user)

你可以看到,~扩大后,由目标shell(由运行root),而$HOME取代由源shell(由经营my_current_user