0

In Bash, I've been trying to get the following content into a file:

for pid in $(ps -lef | grep ssh | grep 2048 | awk '{print $4}'); do kill -9 $pid; done

Doesn't matter how I try to put it, in, say, FileA, it always ends up having the variables substituted by the real values. I've EOF and echo. No luck.

My end goal is to have FileA with the content of the one-liner.

1
  • What is 2048? A port number, PID, PPID, or some command line value? What is your pipeline trying to do? It would be safer with pkill -f ssh -p 2048. Note that your command would match any sshd server that happens to run with a PID or PPID containing the substring 2048.
    – Kusalananda
    Nov 23, 2019 at 16:45

1 Answer 1

2

When you say "I've EOF" I imagine you mean you tried a here document, but have you tried quoting the heredoc word?

cat <<'EOF' >FileA
for pid in $(ps -lef | grep ssh | grep 2048 | awk '{print $4}'); do kill -9 $pid; done
EOF

If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion, the character sequence \newline is ignored, and ‘\’ must be used to quote the characters ‘\’, ‘$’, and ‘`’.

1
  • Perfect! Thank you you so much!
    – t988GF
    Nov 23, 2019 at 16:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .