IFS = $'\\\<br/>'的确切含义是什么? [英] What is the exact meaning of IFS=$&#39;\n&#39;?

查看:133
本文介绍了IFS = $'\\\<br/>'的确切含义是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果以下示例将 IFS 环境变量设置为换行字符...

If the following example, which sets the IFS environment variable to a line feed character...

IFS=$'\n'




  • 美元符号意味着
    完全

  • 在这个特定的$ b $中做了什么b case?

  • 在哪里可以阅读更多关于这个具体的用法(Google不允许在搜索中使用特殊字符,否则我不知道要查找什么)?

    • What does the dollar sign mean exactly?
    • What does it do in this specific case?
    • Where can I read more on this specific usage (Google doesn't allow special characters in searches and I don't know what to look for otherwise)?
    • 我知道 IFS 环境变量是什么,而 \\\
      字符是(换行),但为什么不使用以下格式:
      IFS =\\\
      (这不起作用)?

      I know what the IFS environment variable is, and what the \n character is (line feed), but why not just use the following form: IFS="\n" (which does not work)?

      例如,如果我想循环遍历文件的每一行,并想使用for循环,我可以这样做: / p>

      For example, if I want to loop through every line of a file and want to use a for loop, I could do this:

      for line in (< /path/to/file); do
          echo "Line: $line"
      done
      

      然而,这赢了除非 IFS 设置为换行字符,否则不能正常工作。为了让它工作,我必须这样做:

      However, this won't work right unless IFS is set to a line feed character. To get it to work, I'd have to do this:

      OLDIFS=$IFS
      IFS=$'\n'
      for line in (< /path/to/file); do
          echo "Line: $line"
      done
      IFS=$OLDIFS
      

      注意:我不需要另一种方式来做同样的事情,我已经知道很多其他...我只是好奇那个 $'\\ \\ n',想知道有没有人可以给我一个解释。

      Note: I don't need another way for doing the same thing, I know many other already... I'm only curious about that $'\n' and wondered if anyone could give me an explanation on it.

      推荐答案

      通常 bash 不解释字符串文字中的转义序列。所以如果你写了 \\\
      \\\
      '\\\
      '
      ,这不是一个折线 - 这是字母 n (在第一种情况下)或反斜杠后跟字母 (另外两种情况)

      Normally bash doesn't interpret escape sequences in string literals. So if you write \n or "\n" or '\n', that's not a linebreak - it's the letter n (in the first case) or a backslash followed by the letter n (in the other two cases).

      $'somestring'具有转义序列的字符串文字的语法。所以不像'\\\
      '
      $'\\\
      '
      实际上是一个换行符。

      $'somestring' is a syntax for string literals with escape sequences. So unlike '\n', $'\n' actually is a linebreak.

      这篇关于IFS = $'\\\<br/>'的确切含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆