Why can't I use Unix Nohup with Bash For-loop?

neversaint picture neversaint · Jun 23, 2010 · Viewed 32.7k times · Source

For example this line fails:

$ nohup for i in mydir/*.fasta; do ./myscript.sh "$i"; done > output.txt&
-bash: syntax error near unexpected token `do

What's the right way to do it?

Answer

Jonathan Leffler picture Jonathan Leffler · Jun 23, 2010

Because 'nohup' expects a single-word command and its arguments - not a shell loop construct. You'd have to use:

nohup sh -c 'for i in mydir/*.fasta; do ./myscript.sh "$i"; done >output.txt' &