Using the pattern match !("file1") does not work within a bash script but will work on the command line.
For example:
ls !("file1"|"file2")
Run Code Online (Sandbox Code Playgroud)
This will list all files in directory except file1 and file2.
When that line is executed in a script this error is displayed:
./script.sh: line 1: syntax error near unexpected token `('
./script.sh: line 1: ` ls !("file1"|"file2") '
Run Code Online (Sandbox Code Playgroud)
Regardless what is used rm -v !("file1"). The same error takes place. What is going on …
的行为
!(pattern-list)
Run Code Online (Sandbox Code Playgroud)
特别是在参数扩展中使用时,我的工作方式不正常
${parameter/pattern/string}
Run Code Online (Sandbox Code Playgroud)
a="1 2 3 4 5 6 7 8 9 10"
Run Code Online (Sandbox Code Playgroud)
$ printf "%s\n" "${a/!([0-9])/}"
[blank]
#expected 12 3 4 5 6 7 8 9 10
$ printf "%s\n" "${a/!(2)/}"
[blank]
#expected 2 3 4 5 6 7 8 9 10
$ printf "%s\n" "${a/!(*2*)/}"
2 3 4 5 6 7 8 9 10
#Produces the behaviour expected in previous one, not sure why though
$ printf "%s\n" "${a/!(*2*)/,}"
,2 3 4 5 6 …Run Code Online (Sandbox Code Playgroud) 在我的交互式 shell 中,我可以将模式存储在变量中
$ targets="?(pcm52|pcm61)"
$ ls -l config/devdesc.$targets
-rw-r--r-- 1 kfa kfa 113 Dec 16 13:43 config/devdesc.pcm52
-rw-r--r-- 1 kfa kfa 14 Dec 16 13:44 config/devdesc.pcm61
Run Code Online (Sandbox Code Playgroud)
但是如果我创建一个shell脚本
targets="?(pcm52|pcm61)"
ls -l config/devdesc.$targets
Run Code Online (Sandbox Code Playgroud)
运行它时出现此错误
$ bash -x /tmp/e.sh
+ targets='?(pcm52|pcm61)'
+ ls -l 'config/devdesc.?(pcm52|pcm61)'
ls: cannot access 'config/devdesc.?(pcm52|pcm61)': No such file or directory
Run Code Online (Sandbox Code Playgroud)
为什么放入脚本时会失败?