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

查看:180
本文介绍了为什么在路径脚本中的波浪号不能在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:

  • "Tilde expansion in quotes"
  • "How to manually expand a special variable (ex: ~ tilde) in bash"

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

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