运行此脚本bash ./cleanup.bash
#!/bin/bash
## going to dir moving stuff
rm -rf !(composer.json|.git)
给出错误:
cleanup.bash: line 10: syntax error near unexpected token ‘(‘
cleanup.bash: line 10: ‘rm -rf !(composer.json|.git)’
但如果我直接在终端中运行没有问题rm -rf!(composer.json | .git)
我尝试剥离所有其他行,仍然得到错误.
如何在bash脚本中正确输入?
解决方法:
我想你的问题是由于从脚本运行时没有设置shell扩展的glob选项.当你声称它在命令行中工作时,你已经设置了允许!()globs的extglob标志.
由于每当以#!/ bin / bash启动时bash脚本启动一个新的子shell,父shell中设置的扩展选项可能不会反映在新shell中.为了使其生效,请在剧本之后将其设置在脚本中
#!/bin/bash
shopt -s extglob
## going to dir moving stuff
rm -rf !(composer.json|.git)
标签:bash,linux,terminal
来源: https://codeday.me/bug/20190519/1132719.html