linux空格转义,如何在bash循环列表中转义空格?

首先,不要这样做。最好的办法是正确使用find -exec:

# this is safe

find test -type d -exec echo '{}' +

另一个安全的方法是使用NUL终止列表,虽然这需要你找到支持-print0:

# this is safe

while IFS= read -r -d '' n; do

printf '%q\n' "$n"

done <

您还可以从find中填充数组,并稍后传递该数组:

# this is safe

declare -a myarray

while IFS= read -r -d '' n; do

myarray+=( "$n" )

done <

printf '%q\n' "${myarray[@]}" # printf is an example; use it however you want

如果你的find不支持-print0,你的结果是不安全的 – 如果文件存在包含换行符的名称(这是,是合法的),下面的行为将不会如所期望的:

# this is unsafe

while IFS= read -r n; do

printf '%q\n' "$n"

done <

如果一个人不打算使用上述之一,第三种方法(在时间和内存使用方面效率较低,因为它在字分割之前读取子过程的整个输出)是使用IFS变量不包含空格字符。关闭globbing(设置-f)以防止包含glob字符的字符串,如[],*或?从扩展:

# this is unsafe (but less unsafe than it would be without the following precautions)

(

IFS=$'\n' # split only on newlines

set -f # disable globbing

for n in $(find test -mindepth 1 -type d); do

printf '%q\n' "$n"

done

)

最后,对于命令行参数的情况,你应该使用数组,如果你的shell支持它们(即它的ksh,bash或zsh):

# this is safe

for d in "$@"; do

printf '%s\n' "$d"

done

将保持分离。注意,引用(和使用$ @而不是$ *)很重要。数组也可以通过其他方式填充,如glob表达式:

# this is safe

entries=( test/* )

for d in "${entries[@]}"; do

printf '%s\n' "$d"

done

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值