Let's say you have to echo this into a file:
How do you do it?
My actual "line" to echo is a 2048 characters line.
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
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
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.
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..