bash:〜在shell脚本中扩展 [英] bash: ~ expansion inside shell script

查看:96
本文介绍了bash:〜在shell脚本中扩展的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图编写一个简单的shell脚本来构建用于分析的管道,其中一个步骤是建立到现有目录和文件的符号链接.

I tried to write up a simple shell script to build a pipeline for analysis and one step involves building a symlink to existing directories and files.

最初我是这样设置的:

dir='~/project/xxxx/'
ln -s $dir .

但是,以上代码无效.

我发现〜在执行时并没有在shell脚本中扩展.

I figured it out that the ~ didn't get expanded inside the shell script when it's executed.

我尝试在目录路径周围使用双引号而不是单引号,但这也不起作用.

I tried to use double quote instead of single quote around the directory path but that didn't work either.

以下方法有效

dir="$HOME/project/xxxx/"
ln -s $dir .

我只是想知道为什么〜不起作用.这种现象的正式名称是什么?

I'm just wondering why the ~ didn't work. What's the formal name for this phenomenon?

谢谢!

推荐答案

在任何类型的引号内都不会出现波浪线扩展.因此,例如,使用:

Tilde expansion does not occur inside quotes of any kind. So, for example, use instead:

dir=~'/project/xxxx/'

还请注意,如果使用〜username/格式,则 username/部分也必须取消引用.来自 man bash :

Note also that if you use the ~username/ format, the username/ part must also be unquoted. From man bash:

波浪扩展

   If a word begins with an unquoted tilde character (`~'),  all  of
   the characters preceding the first unquoted slash (or all charac‐
   ters, if there is no unquoted slash) are considered a  tilde-pre‐
   fix.   If  none of the characters in the tilde-prefix are quoted,
   the characters  in  the  tilde-prefix  following  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 exe‐
   cuting the shell is substituted instead.  Otherwise,  the  tilde-
   prefix  is  replaced  with the home directory associated with the
   specified login name.

这篇关于bash:〜在shell脚本中扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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