내장 echo
명령을 통해 ANSI 이스케이프 시퀀스를 사용하여 컬러 텍스트를 인쇄하려고 할 때 \e
제공하는 문자열 의 이스케이프 시퀀스는 “이스케이프”대신 문자 그대로 해석되는 것으로 보입니다 . 이는 Snow Leopard에서만 발생합니다. 아래 예는 Leopard에서 의도 한대로 작동합니다.
스위치 를 사용할 때 올바르게 해석 echo
하기 때문에 -e
스위치를 분명히 지원합니다 \n
.
~ $
~ $ echo "\n"
\n
~ $ echo -e "\n"
~ $
그러나을 사용하려고 \e
하면 다음과 같이 나타납니다.
~ $ echo -e "\e[34mCOLORS"
\e[34mCOLORS
~ $
내가 말했듯이, 레오파드에서 위의 문자열은 “COLORS”라는 문자열을 컬러로 줄 것입니다.
누구나 이것이 의도 된 변경이 될 수있는 이유를 알고 있습니까? Snow Leopard의 Bash 스크립트에서 ANSI 이스케이프 시퀀스를 인쇄하는 방법은 어떻습니까?
Leopard 컴퓨터 3.2.17(1)-release
와 3.2.48(1)-release
Snow Leopard 컴퓨터 의 Bash 셸 버전이 있습니다.
답변
나는 그것이 그 주장을지지하지 않는 이유를 말할 수 없다. 나는 내 리눅스 박스에서만 이것을 얻는다 :
$ /bin/echo --help
Usage: /bin/echo [SHORT-OPTION]... [STRING]...
or: /bin/echo LONG-OPTION
Echo the STRING(s) to standard output.
-n do not output the trailing newline
-e enable interpretation of backslash escapes
-E disable interpretation of backslash escapes (default)
--help display this help and exit
--version output version information and exit
If -e is in effect, the following sequences are recognized:
*emphasized text*
\0NNN the character whose ASCII code is NNN (octal)
\\ backslash
\a alert (BEL)
\b backspace
\c produce no further output
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
NOTE: your shell may have its own version of echo, which usually supersedes
the version described here. Please refer to your shell's documentation
for details about the options it supports.
Report echo bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Report echo translation bugs to <http://translationproject.org/team/>
- 이것은
\e
탈출을 언급하지 않습니다 - 그것은 그것이
/bin/echo
gnu coreutils에서 온 것이라고 말합니다 . Apple이 유닉스 시스템 구성 요소의 소스를 수시로 변경함에 따라 (예 : zsh에서 bash로 이동)/bin/echo
Leopard와 Snow Leopard간에 변경이 있는지 확인하십시오 . gnu 인 경우 gnu.org의 사람들에게 해당 시퀀스를 포함하지 않는 이유를 물어볼 수 있습니다.
해결 방법 (더 흥미로운) :을 사용하지 /bin/echo
않지만 bash의 내장 기능 echo
은 Linux 상자에서 작동합니다. 그들이 내장 에코 (또는 더 모호한 것)없이 bash로 변경했다면, 쉘의 널리 알려진 기능 (적어도 bash 및 zsh에서 작동)을 시도 할 수도 있습니다.
$ echo $'\e[34m''COLORS'
이것은 bash 맨 페이지의 일치하는 부분입니다.
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 an escape character
\f form feed
\n new line
\r carriage return
\t horizontal tab
\v vertical tab
\\ backslash
\' 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
The expanded result is single-quoted, as if the dollar sign had not been present.
A double-quoted string preceded by a dollar sign ($) will cause the string to be trans‐
lated 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.
답변
\x1B
대신 사용해보십시오 \e
.
답변
않는 \033
여전히 작동? 그렇지 않다면 Ctrl + V를 누른 다음 Esc 키 (Mac에 해당 키가있는 경우)를 눌러 명령 줄 안에 실제 제어 문자를 만들 수 있습니다 (편집기에 따라 스크립트에서 제대로 작동하지 않음)
답변
쉘에서 ANSI 이스케이프 시퀀스를 인쇄하는 또 다른 방법은을 사용하는 것 /usr/bin/printf
입니다.
답변
기존의 유용한 답변을 몇 가지 배경 정보로 보완하려면 다음을 수행 하십시오 .
있는 거 당신이 만약 호출 echo
에 의해 이름 만 해당 경로와는 대조적으로, – /bin/echo
– 당신이 배쉬를 호출하고 내장 오히려 외부 유틸리티보다.
내장과 같은 기본 Bash 요소의 동작은 일반적으로 Bash 의미로 이식 가능하므로 Bash 를 실행할 수있는 모든 플랫폼에서 동일하게 작동해야합니다 .
\e
macOS의 3.x Bash 버전에 영향을 미치는 호기심 많은 예외입니다 (v10.13.5 (High Sierra) 현재, macOS에는 법적 이유로 오래된 3.x 버전의 Bash가 제공됨).
\e
(및 별명 \E
) 해야 작동 echo -e
; Bash 2.0에 내장 \e
지원이 추가되었습니다 . echo
하지만 macOS의 3.x 스톡 버전에는 설명 할 수 없습니다.
\e
수행 에 3.X 배쉬 버전에서 작업을 다른 윈도우에 MSYS와 같은 플랫폼.
반대로, 설치 및 맥 OS에 4.x의 강타를 사용하는 경우, \e
수행 작업을.
답변
POSIX를 준수하려고 시도 할 수 있습니다.
http://www.opengroup.org/onlinepubs/9699919799/
부분 옵션은 다음과 같은 상태를 나타냅니다.
Implementations shall not support any options.
답변
참고로, coreutils의 / bin / echo 및 / usr / bin / printf에 \ e 지원을 추가하려고합니다. C 표준은 \ e를 지정하지 않지만 gcc, perl, bash, ksh 및 tcsh는이를 지원합니다.