5

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.

3
  • 2
    You're planning to type echo 2048 random nonsense chars here? I suspect it comes from a file or is generated somehow, yes? Commented Jun 12, 2015 at 15:55
  • @mikeserv , yes. Actually I was trying to make gpg read the passphrase from a file descriptor. Didn't know there was an option for that. My initial "stupid" idea was to echo the passphrase by using an alias in .bash_aliases. Commented Jun 13, 2015 at 17:17
  • Not stupid, just the wrong question. Commented Jun 13, 2015 at 17:26

4 Answers 4

6

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' 
6

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).

0
1

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.

1
  • 4
    That variant (with the delimiter not quoted) still has issues with $, backtick and backslash. Commented Jun 12, 2015 at 15:59
0

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.. 
1
  • Also in 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. Commented Jun 15, 2015 at 9:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.