是波浪号,`~`被认为是相对路径?

我正在尝试提取Nvidia cuda库安装程序的不同部分。 我正在使用以下命令:

mkdir ~/Downloads/nvidia_installers ./cuda_6.5.14_linux_64.run -extract=~/Downloads/nvidia_installers 

我收到以下消息:

 ERROR: extract: path must be absolute. 

当我用我家的文字地址键入命令时,它完美地工作。

 ./cuda_6.5.14_linux_64.run -extract=/home/likewise-open/XXX/username/Downloads/nvidia_installers 

我很困惑不应该〜/ home / similar-open / XXX / username是一样的?

测试:

 ./cuda_6.5.14_linux_64.run -extract=$HOME/Downloads/nvidia_installers 

它有效,但我不知道它为什么不允许~

Bash只会扩展一个〜,如果它是一个单词的开头。 您可以在以下命令之间看到此信息:

 $ echo -extract=~/test -extract=~/test oli@bert:~$ echo -extract ~/test -extract /home/oli/test 

Bash查找独立的~字符和~/替换。 没有其他组合或引用版本可行。

$HOME起作用,因为变量替换更加健壮( $是一个特殊字符,而~则非常少):

 $ echo Thisisastring${HOME}awrawr Thisisastring/home/oliawrawr 

当我们谈论~ ,它实际上还有另外两种替代用途:

  • ~+当前的工作目录(从$PWD读取)
  • ~-上一个工作目录(从$OLDPWD读取)

与简单~ ,这些可以在最后添加额外的路径,并且这些必须是单词的前缀或Bash将忽略它们。

你可以在man bash | less -p ' Tilde'阅读更多相关信息 man bash | less -p ' Tilde'

只是解决它

此命令显示错误消息“错误:提取:路径必须是绝对的”:

 ./cuda_6.5.14_linux_64.run -extract=~/Downloads/nvidia_installers 

该错误没有帮助 – 该程序已经太混乱了。
你已经知道错误是来自~ ,因为它适用于$HOME

问题: ~只在一个单词的开头被替换。

例如,这适用于波浪号:

 echo -extract ~/Downloads 

如果你需要带=的选项语法,使用$ HOME而不是~是最干净的解决方案;

 echo -extract=$HOME/Downloads 

实践

你应该知道的:

有一些特殊情况,当不在一个单词的开头时,将得到扩展:作为变量赋值的一部分,直接在= 。 当然,这在这里令人困惑。

另一个重要的特例是它用于像PATH这样的变量。 在变量赋值中, ~也在以下之后展开: ,就像在第一个=

 $ dir=~ sh -c 'echo D2: $dir' D2: /home/user $ sh -c 'echo D2: $dir' dir=~ D2: $ echo Dir: $dir Dir: $ dir=~; sh -c 'echo D2: $dir' D2: $ echo Dir: $dir Dir: /home/user $ sh -c 'echo D2: $dir'; d3=~ D2: $ echo d3: $d3 d3: /home/user 

代字号的含义

在一个shell中, ~ ,代字号,实际上并不是一条路径。 它只被路径$HOME取代了一段时间。

它类似于shell提供的简写或缩写。
它不能像通常的路径一样使用,shell只能在非常特殊的地方“扩展”到路径。
即使它被扩展,它也可能是除了主目录之外的其他东西。

  • 它只在一个单词开头扩展,或者在:=之后的变量赋值中扩展
  • 如果它不在引号内,它只会被扩展
  • 如果在/之前没有其他字符 ,它只会扩展为$HOME

命令行中的问题

根据这个,你的命令中的问题是波形符号

-extract=~/Downloads/nvidia_installers

没有扩展,因为它不是列出的案例之一。 就这样。

解决方案可能是使代字号成为单词的第一个不带引号的字符,在下一个/之前没有其他字符 – 这就是在选项参数之前使用带空格的选项时得到的结果:

-extract ~/Downloads/nvidia_installers

另一种解决方案是使用$HOME代替。 在脚本中,这通常是更好的选择。

-extract=$HOME/Downloads/nvidia_installers

错误消息

但是错误信息是怎么回事
"ERROR: extract: path must be absolute."
适合这一切?

我们知道波浪号没有扩大。 这意味着程序得到的参数文本包括~ ,但没有/home/auser作为路径。 那个路径是~/Downloads/nvidia_installers – 但现在没有shell,所以代字号没有特殊含义。 它只是一个普通的目录名称。 并且作为foo/bar/bazforms的每个其他路径,它是一个相对路径

其他用途

如果在~之后有字符,就像在~alice – 上面有所有其他规则 – 并且有一个用户名alice ,那就是扩展到alice的主目录,比如home/alice
另外,如果你是bob~会扩展到/home/bob~bob会扩展为相同。

变量~+扩展到当前目录$PWD

要引用上一个目录,即最后一个cd之前的位置,可以使用~- ,它扩展为$OLDPWD

如果你使用pushdpopd而不是cd ,你就会知道可以像~-2那样访问目录堆栈。

细节

所有~扩展到路径的情况都由shell进行处理 。 对于其他程序, ~只是一个普通的文件名字符。

对于shell中的确切定义 ,这里是man bash的相关部分
注意如何用$HOME替换~只是一种特殊情况: “如果这个登录名是空字符串,则用shell参数HOME的值替换代字号。”

 Tilde Expansion If a word begins with an unquoted tilde character (`~'), all of the charac‐ ters preceding the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix. If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix follow‐ ing the tilde are treated as a possible login name. If this login name is the null string, the tilde is replaced with the value of the shell parame‐ ter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead. Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name. If the tilde-prefix is a `~+', the value of the shell variable PWD replaces the tilde-prefix. If the tilde-prefix is a `~-', the value of the shell variable OLDPWD, if it is set, is substituted. If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a `+' or a `-', the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs builtin invoked with the tilde-prefix as an argument. If the characters following the tilde in the tilde-prefix consist of a number without a leading `+' or `-', `+' is assumed. If the login name is invalid, or the tilde expansion fails, the word is unchanged. Each variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =. In these cases, tilde expansion is also per‐ formed. Consequently, one may use filenames with tildes in assignments to PATH, MAILPATH, and CDPATH, and the shell assigns the expanded value. 

~本身不是路径。 这是一个从shell获得特殊处理的角色,其中~~/表示“替换为当前用户的主目录路径”。 ~username表示“用用户名的主目录路径替换”。

因为它不是路径,所以它只在命令的某些位置被识别(作为新的空间分割标记的第一个字符)。

展开时,它用绝对路径替换。

使用$HOME有效的,因为HOME只是由shell设置的变量,并且遵循用于变量替换的正常shell规则(它在输入被分割为空格并执行之前发生)。

你是对的。 〜/ Downloads与/ home / username / Downloads相同。

一些安装程序和提取程序只是非常挑剔它需要放置的东西。 我想这可能是因为它记录了文件路径,并且日志不会在接受的路径中接受〜。

我刚习惯输入/ home / username。 🙂