Ubuntu 명령 줄에서 따옴표를 사용하는시기와 작은 따옴표 및 큰 따옴표가 필요한시기를 이해하는 간단한 규칙이 있습니까?
예를 들어 여기서 차이점을 설명하는 방법은 다음과 같습니다.
$ echo $'a\naa\nac\nb\ncc\tdd\ne\se'
a
aa
ac
b
cc dd
e\se
$
과
$ echo $"a\naa\nac\nb\ncc\tdd\ne\se"
a\naa\nac\nb\ncc\tdd\ne\se
$
귀하의 경우 특수 인용 구문, 즉 $'...'
및 $"..."
. 이 구문은 Korn 쉘에서 Zsh 및 Bash로 왔으며 현재 POSIX에 있습니다 (예를 들어 http://mywiki.wooledge.org/Bashism 의 "expand sequence"행 참조 ). 이 구문은 명령 줄에서 큰 따옴표 "", 작은 따옴표 ''및 백틱 ´ ´의 차이점 에 대한 답변에 언급되지 않았습니다 . .
이 구문에 대한 자세한 내용은 bash (1) 맨 페이지에서 찾을 수 있습니다.
Words of the form $'string' 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
\E an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' single quote
\" double 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)
\uHHHH the Unicode (ISO/IEC 10646) character whose value is
the hexadecimal value HHHH (one to four hex digits)
\UHHHHHHHH
the Unicode (ISO/IEC 10646) character whose value is the
hexadecimal value HHHHHHHH (one to eight hex digits)
\cx a control-x character
The expanded result is single-quoted, as if the dollar sign had not
been present.
A double-quoted string preceded by a dollar sign ($"string") will
cause the string to be translated according to the current locale. If
the current locale is C or POSIX, the dollar sign is ignored. If the
string is translated and replaced, the replacement is double-quoted.
이것은 또한 변수가에서 확장 $"..."
되지만에서는 확장 되지 않음을 의미 $'...'
합니다. 비교하십시오 :
$ echo 'I am using\t$SHELL.\n'
I am using\t$SHELL.\n
$ echo $'I am using\t$SHELL.\n'
I am using $SHELL.
$ echo "I am using\t$SHELL.\n"
I am using\t/bin/bash.\n
$ echo $"I am using\t$SHELL.\n"
I am using\t/bin/bash.\n
첫 번째 예에서는 $SHELL
변수 나 백 슬래시 이스케이프가 확장되지 않습니다. 두 번째 예에서 백 슬래시 예는 확장되지만 변수는 확장되지 않습니다. 세 번째와 네 번째 예는 동일한 결과를 제공합니다. 변수 만 확장됩니다.
이 양식 $'...'
은 \t
또는 \n
변수 와 같은 메타 문자가있는 내용을 할당하는 데 유용 할 수 있습니다 . $"..."
그래도 양식에 대한 신청서를 찾을 수 없습니다 .
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다