Home > database >  nohup with nested quotes and subshell
nohup with nested quotes and subshell

Time:06-08

My goal is to interpolate some output from a pipeline into a string, then append the string to a file:

echo "some text $(aws ... | tail -1)" >> myfile.txt"

That works fine. Now I'd like run that in the background and to keep running when I exit the shell:

nohup bash -c "echo \"some text $(aws ... | tail -1)\" >> myfile.txt" &

However, this results in only "some text " being appended.

CodePudding user response:

you didn't escape the $ of $(aws ... | tail -1), so the current shell expands it and bash -c ends up executing something weird like echo "some text {"hello": "world"}" >> myfile.txt

The easy fix is to use single quotes:

nohup bash -c 'echo "some text $(aws ... | tail -1)" >> myfile.txt' &

CodePudding user response:

Do not invest your time in unreadable quoting. Define a function. Export that function. Run that function in bash.

func() {
  echo "some text $(aws ... | tail -1)" >> myfile.txt
}
export -f func
nohup bash -c func &
  •  Tags:  
  • bash
  • Related