路径中「~」的意思是什么?

2016-05-04 17:25:51 +08:00
 pOH8
$ cd /Users/zhanglei/工作库 /Demo/LuceneDemo/~/Downloads/LucenseDemo

上面这条命令中「~」是什么意思?

$ rm -rf /Users/zhanglei/工作库/Demo/LuceneDemo/~/
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory

$ cd ..
cd: Could not set PWD variable
shell-init: error retrieving current directory: getcwd: cannot access parent directories: No such file or directory
8709 次点击
所在节点    Linux
13 条回复
julyclyde
2016-05-04 17:31:37 +08:00
在某些 shell 里,波浪号代表$HOME 这个意思
n6DD1A640
2016-05-04 17:34:25 +08:00
指你当前用户的 $HOME 目录。如果真的需要删除一个带 ~ 字符的文件 /目录,需要反斜杠 \
misaka19000
2016-05-04 17:36:23 +08:00
比如你用 root 用户登录,辣么 cd ~就意味着打开 /home/root ,其他用户也是同样的道理
secfun
2016-05-04 17:36:34 +08:00
是 当前用户目录的别名
debiann
2016-05-04 17:39:44 +08:00
什么情况下会有楼主这种命令?还是说这只是楼主随便写的?
Havee
2016-05-04 17:43:32 +08:00
root 的 home 为 /root
bp0
2016-05-04 18:02:15 +08:00
怎么会有这么奇怪的路径,一般~都是放在最前面的,比如 cd ~/test/ 表示进入当前用户的 home 目录下的 test 。

一般用户的 home 目录为 /home/username/

root 的 home 目录为 /root
irenicus
2016-05-04 18:35:15 +08:00
你家!(严肃脸)
SoloCompany
2016-05-04 21:24:37 +08:00
~ 是由 shell 解释的
只有在特定语境下,才会被自动替换成 ${HOME}

特定语境是指
- 必须是作为任意参数(包括 $0 )的开始字符
- 后面要么没有内容要么紧跟着一个 /

示例
echo $HOME
/Users/foo

echo a-$HOME-b
a-/Users/foo-b

echo ~
/Users/foo

echo ~/
/Users/foo

echo ~/b
/Users/foo/b

echo a-~
a-~

echo ~-b
~-b
raysonx
2016-05-04 21:27:56 +08:00
@SoloCompany ~username 也是可以的
SoloCompany
2016-05-04 21:29:48 +08:00
@raysonx 对!我忘了这个了,后置字符的也要满足同样的规则
satoru
2016-05-05 08:01:27 +08:00
我昨天刚删了这个目录
hei1000
2016-05-05 11:50:46 +08:00
有可能是你不小心新建的一个名字叫~的文件夹,删除的时候要小心了,之前我不小心新建过名字叫-"的文件夹

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/276309

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX