IFS 在 Bash 中如何工作?

发布于 2024-10-29 12:09:36 字数 550 浏览 4 评论 0 原文

#!/bin/bash
# This question is from advanced bash scripting guide section 5.1

echo
var="'(]\\{}\$\""
IFS='\'
echo $var

    # output is '(] {}$"     
    # \ converted to space. Why?

echo "$var"      

    # output is '(]\{}$"      
    # special meaning of \ used, \ escapes \ $ and " RIGHT?

echo
var2="\\\\\""
echo $var2       

    # output is   "         
    # \ converted to space. Why?

echo

    # But ... var2="\\\\"" is illegal. Why?

var3='\\\\'
echo "$var3"     # \\\\

    # Strong quoting works, though. Why?
#!/bin/bash
# This question is from advanced bash scripting guide section 5.1

echo
var="'(]\\{}\$\""
IFS='\'
echo $var

    # output is '(] {}$"     
    # \ converted to space. Why?

echo "$var"      

    # output is '(]\{}$"      
    # special meaning of \ used, \ escapes \ $ and " RIGHT?

echo
var2="\\\\\""
echo $var2       

    # output is   "         
    # \ converted to space. Why?

echo

    # But ... var2="\\\\"" is illegal. Why?

var3='\\\\'
echo "$var3"     # \\\\

    # Strong quoting works, though. Why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-11-05 12:09:36
  ifs ='\'
回声$ var        

    #o/p是'(] {} $”     

    #\转换为空间。为什么?

因为您告诉 shell 反斜杠是字段分隔符,并且在 echo 出它时没有引用 $var,所以它受到 基于 IFS 的分词

  echo“ $ var”      

    #o/p是'(] \ {} $”      
    # \ 使用的特殊含义,\ 转义 \ $ 和 " 对吗?

在这里,您引用了 $ var ,因此no word拆分将在上面执行。您的输出正是您告诉 shell var 的内容。即'(] \ {} $“

  var2 =“ \\\\\”“”

Echo $ var2       

    #o/p是”        
    #\转换为空间。为什么?

请参阅第一个答案

 ##但是... var2 =“ \\\\”“”是非法的。为什么?

因为每对后斜线都构成了字面的后斜线,并且没有剩下的后斜线来逃脱第二个双引号。外壳不知道该如何处理3个双引号。

  echo“ $ var3”#\\\\ \

    # 不过,强引用是有效的。为什么 ?

有关

请参阅 $''var=$'\'(]\{}$"' 相比,这只需要您转义单引号

IFS='\'
echo $var        

    # o/p is '(] {}$"     

    # \ converted to space. Why?

Because you told the shell that a backslash is a field separator and since you did not quote $var when you echo'd it out, it was subject to word splitting based on IFS.

echo "$var"      

    # o/p is '(]\{}$"      
    # special meaning of \ used, \ escapes \ $ and " RIGHT ?

Here you quoted $var and thus no word splitting will be performed on it. Your output is exactly what you told the shell var was equal to. i.e. '(]\{}$"

var2="\\\\\""

echo $var2       

    # o/p is   "        
    # \ converted to space. Why?

See first answer

# But ... var2="\\\\"" is illegal. Why?

Because every pair of backslashes makes up a literal backslash and there is no backslash left over to escape out the 2nd double quote. The shell doesn't know what to do with 3 double quotes.

echo "$var3"     # \\\\

    # Strong quoting works, though. Why ?

See second answer about word splitting

Note that you could also use the string literal syntax $'' vis var=$'\'(]\{}$"' which would only require you to escape out the single quote

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文