简体   繁体   English

Shell脚本第一行中的环境变量扩展

[英]environment variable expansion in first line of shell script

My ruby shell scripts specify the ruby interpreter in the first line of the script as: 我的ruby shell脚本在脚本的第一行中将ruby解释器指定为:

#!/Users/me/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

The problem is that when I upgrade to a new ruby version, I have to edit all the script files to update the interpreter. 问题是,当我升级到新的ruby版本时,我必须编辑所有脚本文件以更新解释器。 There is an environment variable available, $MY_RUBY_HOME, that expands to the current path (minus the /bin/ruby part). 有一个可用的环境变量$ MY_RUBY_HOME,它会扩展到当前路径(减去/ bin / ruby​​部分)。 However, all my attempts to use: 但是,我的所有尝试使用:

#!{$MY_RUBY_HOME}/bin/ruby
#!${MY_RUBY_HOME}/bin/ruby
etc

fail ("bad interpreter: No such file or directory"). 失败(“错误的解释器:没有这样的文件或目录”)。 I suspect environment expansion isn't done on the first line, so I may just be out of luck. 我怀疑环境扩展在第一行还没有完成,所以我可能很走运。 I would be interested if anybody has been able to use environment variable expansion on the program definition line in a shell script. 如果有人能够在Shell脚本的程序定义行中使用环境变量扩展,我将很感兴趣。

Put ${MY_RUBY_HOME}/bin in your $PATH and use #!/usr/bin/env ruby . ${MY_RUBY_HOME}/bin放入$PATH并使用#!/usr/bin/env ruby See here . 这里

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM