0

This example not work in my case. This example is about bash -c but there is no cat example.

How Escaping a dollar sign using bash -c ? This is my code. It is not work correct. I get empty file. But I want text $6 in file.

bash -c "cat <<EOF > /home/pi/file.txt
\$6
EOF"

P.S. I need bash -c because I plan to use cat with sudo.

  • should be `bash -c "cat > /tmp/file" << EOF` – pynexj May 15 '23 at 06:08
  • I guess "want $6" means that you want $6 be replaced by the 6th parameter of the calling function/script. In this case, don't escape it. If it is empty, then you don't have at least 6 parameters on the calling side. – user1934428 May 15 '23 at 06:19
  • @pynexj : I'm not convinced that your comment is correct. AFIK, just the next **word** after << is taken as the delimiter for the here-document. Therefore it is safe to add another redirection (or even other parameters) after the < – user1934428 May 15 '23 at 06:21
  • @pynexj : The lines in the HERE-Doc are bit intended for the command _bash_. They are intended as stdin for `cat`, i.e file `deleteFile` should be created having $6 as content. Basically the OP wants to achieve the effect of `echo "$6" >~/deleteFile` in a cumbersome way. – user1934428 May 15 '23 at 06:31
  • 2
    ok. i think OP should just use single quotes here. or use `\\\$6` for double quotes. – pynexj May 15 '23 at 06:32
  • @pynexj \\\$6 works. Can you explain why I need 3 times back slash, not once? And what you mean "should just use single quotes here"? Give example please – Андрей Тернити May 15 '23 at 08:23
  • see https://i.stack.imgur.com/kwb6T.png – pynexj May 15 '23 at 09:27
  • This seems like an [XY Problem](https://en.wikipedia.org/wiki/XY_problem). Probably you are looking for something like `printf '%s\n' '$6' 'foo' 'bar' 'baz' | sudo tee /path/to/file >/dev/null` – tripleee May 15 '23 at 09:33

0 Answers0