문자열을 $
to로 직접 전달echo
$ echo $'#include <iostream>\nint main() {\n std::cout << \"Hello World!\" << std::endl;\n}'
내장된 ANSI 이스케이프 시퀀스를 확장합니다.
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
}
문자열을 변수에 할당했습니다.
codeStr='#include <iostream>\nint main() {\n std::cout << \"Hello World!\" << std::endl;\n}'
그런 다음 변수를 에코합니다.
echo $codeStr
서식이 지정된 텍스트가 아닌 원시 문자열을 얻었습니다.
문자열을 직접 전달할 때와 같이 서식이 지정된 텍스트를 얻으려면 어떻게 해야 합니까?
답변1
사용-e
로 전환enable interpretation of backslash escapes
.
$ codeStr='#include <iostream>\nint main() {\n std::cout << \"Hello World!\" << std::endl;\n}'
$ echo -e $codeStr
#include <iostream>
int main() {
std::cout << \"Hello World!\" << std::endl;
}
@steeldriver 주석을 처리하면 printf
작동합니다(시퀀스를 올바르게 해석합니다).
$ codeStr='#include <iostream>\nint main() {\n std::cout << \"Hello World!\" << std::endl;\n}'
$ printf "$codeStr"
#include <iostream>
int main() {
std::cout << "Hello World!" << std::endl;
}