Search code examples
backgroundcommand

How to process a command with if condition in background in command line on linux


I want to run the following command in background:

if [ ! -e test.txt ]; then echo test; else echo test1 && echo test2; fi;

I tried:

if [ ! -e test.txt ]; then echo test; else echo test1 && echo test2 > /dev/null 2>&1 &; fi;

But it gives me the error: -bash: syntax error near unexpected token ;'` I also tried:

if [ ! -e test.txt ]; then echo test; else echo test1 && echo test2; fi; > /dev/null 2>&1 &

It worked but not in the background. Is there any method to do that? Thanks in advance!


Solution

  • I tried to use:

     if [ ! -e test.txt ]; then echo test; else echo test1 && echo test2; fi > /dev/null 2>/dev/null &
    

    And it seems to work now