为什么路径中的波浪号没有在 shell 脚本中展开? [英] Why is a tilde in a path not expanded in a shell script?

查看:34
本文介绍了为什么路径中的波浪号没有在 shell 脚本中展开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让 Android Studio 启动器 (studio.sh) 使用我手动安装的 Java(不是系统范围的默认 Java).因为我已经在我的 .bashrc 文件中声明了 PATH 和 JAVA_HOME,所以我只是在 shell 脚本中找到了那个文件:

I tried to get the Android Studio launcher (studio.sh) to use my manually installed Java (not the system-wide default Java). Since I already declared PATH and JAVA_HOME in my .bashrc file, I simply sourced that file in the shell script:

./home/foobar/.bashrc

. /home/foobar/.bashrc

但由于某种原因,脚本仍然无法将 $JAVA_HOME/bin/java 识别为可执行文件.

but for some reason, $JAVA_HOME/bin/java was still not recognized as an executable file by the script.

我添加了一些日志,发现 JAVA_HOME 被扩展为 ~/install/java...,即波浪号运算符没有扩展到主目录.

I added some logging and found out that JAVA_HOME was expanded as ~/install/java..., i.e. the tilde operator was not expanded into the home directory.

我进行了一些搜索,但找不到任何未扩展的原因.波浪号是 Bash 特有的功能吗(脚本使用 #!/bin/sh,Linux Mint 使用破折号,而不是 bash)?波浪号在某些情况下不起作用吗?

I did some searching, but couldn't find any reason why it was not expanded. Is tilde a Bash-specific feature (the script uses #!/bin/sh, and Linux Mint uses dash, not bash)? Does tilde not work in some circumstances?

我在我的 .bashrc 声明中用 $HOME 替换了 ~ ,然后它起作用了,所以 HOME 在运行时是已知的.

I replaced ~ with $HOME in my .bashrc declaration, and then it worked, so HOME is known at runtime.

推荐答案

bash 手册,注意括号扩展期间参数替换,但不是递归的:

In the bash manual, note that brace expansion during parameter substitution, but not recursively:

展开的顺序是:大括号展开;波浪号扩展、参数和变量扩展、算术扩展和命令替换(以从左到右的方式完成);分词;和文件名扩展.

The order of expansions is: brace expansion; tilde expansion, parameter and variable expansion, arithmetic expansion, and command substitution (done in a left-to-right fashion); word splitting; and filename expansion.

这意味着任何未展开存储在 bash 变量中的波浪号(或参数引用或命令替换)将不会自动解析.您的 JAVA_HOME 变量包含文字波浪号,因此 bash 不会自动扩展它.

This implies that any tilde (or parameter references or command substitution) stored unexpanded in a bash variable will not automatically resolve. Your JAVA_HOME variable contains a literal tilde, so bash will not expand it automatically.

您的修复很可能有效,因为波浪号扩展不适用于引号:

It is likely that your fix worked because tilde expansion does not apply in quotes:

$ echo "~"
~
$ echo ~
/home/jeffbowman

...但是像 $HOME 这样的参数扩展确实出现在引号中.在分配 JAVA_HOME 期间,用 $HOME 替换它会扩展到您的主目录.

...but parameter expansion like $HOME does occur in quotes. Replacing it with $HOME expands to your home directory during the assignment of JAVA_HOME.

FOO=~/bar        # stores /home/jeffbowman/bar
FOO="~/bar"      # stores ~/bar
FOO=$HOME/bar    # stores /home/jeffbowman/bar
FOO="$HOME/bar"  # stores /home/jeffbowman/bar

虽然更好的选择是确保您的作业正确无误,但如果您想手动扩展它,这些 SO 问题有一些不错的选择:

Though the better option is to ensure your assignment is correct, if you want to expand it manually, these SO questions have some good options:

这篇关于为什么路径中的波浪号没有在 shell 脚本中展开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆