prevent变量代换后通配符 [英] Prevent globbing after variable substitution
问题描述
什么是使用包含保留通配符(文件名完成)触发一些不需要替换字符shell变量(BASH)最优雅的方式?这里是例子:
What is the most elegant way to use shell variable (BASH) that contain characters reserved for globbing (filename completion) that trigger some unwanted substitutions? Here is the example:
for file in $(cat files); do
command1 < "$file"
echo "$file"
done
文件名包含像'['或']'字符。我有两种基本思路:
The file names contain characters like '[' or ']'. I have basically two ideas:
1)关闭通过设置-f通配符:我需要在别的地方
1) Turn off globbing via set -f: I need it somewhere else
2)在逃亡文件的文件名:BASH抱怨找不到文件管道进入标准输入时
2) Escape the file names in files: BASH complains about "file not found" when piping into stdin
THX任何建议。
的编辑:唯一的答案缺少的是如何从包含用于通配符时,文件名是在shell变量$文件,E特殊字符名称的文件读取。 G。命令1&LT; $文件。的
推荐答案
作为一种替代之间切换设置-f
和设置+ F
你可以或许只是申请一个设置-f
来一个子shell,因为父shell的环境不会受到受此可言:
As an alternative to switching between set -f
and set +f
you could perhaps just apply a single set -f
to a subshell since the environment of the parent shell would not by affected by this at all:
(
set -f
for file in $(cat files); do
command1 < "$file"
echo "$file"
done
)
# or even
sh -f -c '
for file in $(cat files); do
command1 < "$file"
echo "$file"
done
'
这篇关于prevent变量代换后通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!