Ami*_*oui 2 shell bash shell-script quoting variable
bash 版本:
$ bash -version
GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Run Code Online (Sandbox Code Playgroud)
命令:
$ var="[a-p]"; echo $var
h
Run Code Online (Sandbox Code Playgroud)
任何人都知道这种解释的原因是什么。
对于上下文,我正在使用 shell 解析配置文件并寻找模式来获取部分名称
[section-foo]
foo=bar
Run Code Online (Sandbox Code Playgroud)
在读取文件时,例如,如果某个部分行是 [ap],则会出现这种奇怪的行为。
您h在运行的目录中有一个文件名
echo $var
Run Code Online (Sandbox Code Playgroud)
shell 尝试[a-p]用作 glob,如果有任何匹配,它将用匹配替换 glob。使用名为 的文件h,它变成
echo h
Run Code Online (Sandbox Code Playgroud)
您可以通过引用扩展来防止这种情况,这将导致它不被视为一个 glob。
echo "$var"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
125 次 |
| 最近记录: |