bash 是否使用正则表达式搜索?
Does bash search with regex?
bash 是否使用正则表达式搜索?
例如,如果我想做一个 for 循环遍历当前目录中以字母 a
开头的所有文件,下面的方法是否可行?
for x in a.*
do
当谈到 文件名扩展 时,ABS 指出:
Bash itself cannot recognize Regular Expressions.
Inside scripts, it
is commands and utilities -- such as sed
and awk
-- that interpret
RE's.
Bash does carry out filename expansion -- a process known as
globbing, but this does not use the standard RE set. Instead, globbing recognizes and expands wild cards.
然而,正如 chepner, bash DOES support regex 在 filename expansion
上下文之外提到的:
(digit=5; [[ $digit =~ [0-9] ]] && echo match)
回到 OP 的问题,遍历以 a
:
开头的所有文件
for x in a* ; do
[[ -f "$x" ]] || continue
# process x
done
bash 是否使用正则表达式搜索?
例如,如果我想做一个 for 循环遍历当前目录中以字母 a
开头的所有文件,下面的方法是否可行?
for x in a.*
do
当谈到 文件名扩展 时,ABS 指出:
Bash itself cannot recognize Regular Expressions.
Inside scripts, it is commands and utilities -- such assed
andawk
-- that interpret RE's.Bash does carry out filename expansion -- a process known as globbing, but this does not use the standard RE set. Instead, globbing recognizes and expands wild cards.
然而,正如 chepner, bash DOES support regex 在 filename expansion
上下文之外提到的:
(digit=5; [[ $digit =~ [0-9] ]] && echo match)
回到 OP 的问题,遍历以 a
:
for x in a* ; do
[[ -f "$x" ]] || continue
# process x
done