Let's say you have to echo this into a file:
RZW"a4k6[)b!^"%*X6Evf How do you do it?
My actual "line" to echo is a 2048 characters line.
Just put it in single quotes:
echo 'RZW"a4k6[)b!^"%*X6Evf' > file But if you have any single quotes in the string you need to escape each of those in double quotes ("'") and "glue" the result together like this:
echo 'text without single quotes'"'"'and other text without single quote' For arbitrary content, you can also use:
cat << 'EOF' > file RZW"a`4$k6[)b!^"'%*X6Evf RZW"a`4$k6[)b!^"'%*X6Evf EOF (as long as that content doesn't contain lines that consist of exactly EOF, in which case you can use a different delimiter).
To include NUL characters (^@ usually entered with Ctrl+VCtrl+Space or Ctrl+VCtrl+@), the above will only work with zsh. With other shells (and zsh), you can do:
cat > file Enter the text and hit Ctrl+D on an empty line (or Ctrl-D twice if you want to include a non-terminated line) when finished.
Another approach to deal with arbitrary, potentially binary data is to use things like uuencode or base64:
For instance:
printf '\0\1\2\3' > file Can be written:
uudecode << 'EOF' begin 644 file $``$"`P`` ` end EOF (that output being obtained by running uuencode file < some-file where some-file contains that particular content).
I would use a "here document".
cat >my_file <<__EOF__ RZW"a4k6[)b!^"%*X6EvF __EOF__ When the shell sees the <<foo syntax, it remembers what foo is and continues reading until it sees foo on a line by itself. Then it passes everything it read (except for foo) to the program's standard input.
$, backtick and backslash. To generate something to use as a command line argument, put it into a variable (or something else that you can ensure it gets into a word — then a single argument that gets passed to printf), then let bash/ksh93 do the rest.
# Reading from stdin # pbpaste | bash -c ' printf echo\ %q\\n "$(cat)" # ' | pbcopy # Then paste it into your script and echo happily. # Maybe I should use the `read' builtin instead.. zsh. Note that it removes all trailing newline characters and adds one. It won't work with NUL characters except with zsh. You probably don't want to use echo here as you'll have trouble with values like -n/-e or containing backslash depending on the echo implementation or the environment.
echo2048 random nonsense chars here? I suspect it comes from a file or is generated somehow, yes?