yzc577亚洲城娱乐一个shell的基础编程问题

( yzc577亚洲城娱乐 )

如下的代码:

shellIFS=:
IFS=$'\n'
IFS=$'\n;='

给IFS赋值,为什么第2,3行要用到$号,为什么第一行不用,$号有什么作用吗?

Words of the form $aqstringaq are treated specially. The word expands to string, with backslash-escaped characters replaced as specified by the ANSI C standard. Backslash escape sequences, if present, are decoded as follows:

\a
    alert (bell) 
\b
    backspace 
\e
    an escape character 
\f
    form feed 
\n
    new line 
\r
    carriage return 
\t
    horizontal tab 
\v
    vertical tab 
\\
    backslash 
\aq
    single quote 
\nnn
    the eight-bit character whose value is the octal value nnn (one to three digits) 
\xHH
    the eight-bit character whose value is the hexadecimal value HH (one or two hex digits) 
\cx
    a control-x character

shell小白一只,简单test了一下:

  #! /bin/bash
  IFS=:
  echo "$IFS"
  IFS='\n'
  echo "$IFS"
  IFS=$'\n'
  echo "$IFS"
  IFS=$'\n;='
  echo "$IFS"

Shell script的输出是:

:
\n


;=

具体$和不加$造成上述输出的原理我也不清楚,求其他大神解答~

加$符号将现执行’\n’转义,既将单引号”内的\n转为换行.而$则是获取这个转义后的结果,既为一个空行.
如果不使用$的话,eho “$IFS”将打印原始字符,因为”‘\n'”中的’\n’被视作一个普通字符串.

(看完/读完)这篇文章有何感想! yzc577亚洲城娱乐的分享…

发表评论

电子邮件地址不会被公开。 必填项已用*标注