Score:0

How do I get the formatted text as in direct passing a string?

th flag

direct passing a string with $ to echo

$ echo $'#include <iostream>\nint main() {\n  std::cout << \"Hello World!\" << std::endl;\n}'

expands the embedded ANSI escape sequences

#include <iostream>
int main() {
  std::cout << "Hello World!" << std::endl;
}

I assigned the string to a variable

codeStr='#include <iostream>\nint main() {\n  std::cout << \"Hello World!\" << std::endl;\n}'

and then echo the variable

echo $codeStr

I got the raw string rather than a formatted text.

How do I get the formatted text as in direct passing a string?

Score:1
sd flag

Use -e switch to 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;
}
hr flag
... note that whereas `$'...'` treats `\"` as an escape sequence, the bash shell's builtin `echo -e` doesn't (hence the output here is actually different from the OP's). See also [Why is printf better than echo?](https://unix.stackexchange.com/questions/65803/why-is-printf-better-than-echo)
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.