Bash 终端和 shell 脚本之间的 glob 模式行为不同
Bash glob pattern behaviour different between terminal and shell script
我有一个 glob 模式 {,**/}*.*
用于递归当前目录中的所有文件以及子文件。在终端中,如果我 运行 echo {,**/}*.*
它输出当前目录中的所有文件以及嵌套目录。当我 运行 一个包含这一行的 shell 脚本时,它只执行一个目录深度。
我明白 terminal has different behaviour than the shell:添加 shopt -s extglob
没有任何区别。
#!/bin/bash
shopt -s extglob
echo {,**/}*.*
我在 MacOSX 上启用了 Bash 4 terminal 和 shopt -s globstar
。
{,**/}*.*
不会为您提供 当前目录中的所有文件以及子文件。 相反,它会为您提供格式为
的结果
directory/file.ext
您可能使用了 ./**
而不是
如果您使用 globstar,**
将扩展到文件夹中的所有内容。即
shopt -s globstar
echo ./** # this would suffice
除此之外,借用 comment, you need to use a shell version that supports globstar. This is evident from you
shopt: globstar: invalid shell option name
然后,
#!/bin/bash -> #!/usr/local/bin/bash
应该做这份工作
感谢@Aserre 和@anubhava,这确实是 bash 路径的组合,并确保启用了 globstar(对于 MacOSX)。完整的脚本是:
#!/usr/local/bin/bash
shopt -s globstar
echo {,**/}*.*
是的,./**
就足够了,但这不是我的问题:)
我有一个 glob 模式 {,**/}*.*
用于递归当前目录中的所有文件以及子文件。在终端中,如果我 运行 echo {,**/}*.*
它输出当前目录中的所有文件以及嵌套目录。当我 运行 一个包含这一行的 shell 脚本时,它只执行一个目录深度。
我明白 terminal has different behaviour than the shell:添加 shopt -s extglob
没有任何区别。
#!/bin/bash
shopt -s extglob
echo {,**/}*.*
我在 MacOSX 上启用了 Bash 4 terminal 和 shopt -s globstar
。
{,**/}*.*
不会为您提供 当前目录中的所有文件以及子文件。 相反,它会为您提供格式为
directory/file.ext
您可能使用了 ./**
而不是
如果您使用 globstar,**
将扩展到文件夹中的所有内容。即
shopt -s globstar
echo ./** # this would suffice
除此之外,借用
shopt: globstar: invalid shell option name
然后,
#!/bin/bash -> #!/usr/local/bin/bash
应该做这份工作
感谢@Aserre 和@anubhava,这确实是 bash 路径的组合,并确保启用了 globstar(对于 MacOSX)。完整的脚本是:
#!/usr/local/bin/bash
shopt -s globstar
echo {,**/}*.*
是的,./**
就足够了,但这不是我的问题:)