0

I can't make this line to work on my bash script:

echo "VAR=$(ps ax | grep myprocess)" >> myprocess

How should I do to escape the $(ps ax | grep myprocess) expansion?

I want as a result, a myprocess file with this content:

VAR=$(ps ax | grep myprocess)

EDIT: The example I used is too simple, but it was intended to show the specific problem. Here is the real command:

su - -c "echo \"#!/bin/bash\nPID=$(ps ax | grep elasticsearch-${VERSION}.jar | grep -v grep | cut -f1 --delimiter=\" \" -s)\nif [ -n \"\${PID}\" ];\nthen\n\tkill \${PID}\n\techo \"ElasticSearch process killed.\"\nelse\n\techo \"ElasticSearch is not running.\"\nfi\" >> /home/user/start_elastic" user

Some considerations:

  • I can't change to single quotes, because ${VERSION} would not be replaced.
  • I tried adding a backslash like this: \$(, but it didn't worked. the evaluation is executed before creating the start_elastic file.
Facundo Chambo
  • 3,088
  • 6
  • 20
  • 25

0 Answers0