I want to run a lot of python scripts and save their outputs. Because running one by one in terminal is too cumbersome. As suggested, I decide to use a shell script (.sh).

To simplify the problem, let's say I have two python scripts to run (print1.py and print2.py). Script print1.py outputs number 1 and script print2.py outputs number 2.

If not using the shell script (i.e. enter commands separately), I can get the desired result.

If not using the shell script, the commands I would use are

$ nohup python print1.py &> 1.txt &
$ nohup python print2.py &> 2.txt &

which redirect the output to 1.txt and 2.txt, respectively. So 1.txt would contain a number 1, and 2.txt would contain a number 2. It works well if I enter these two commands separately in the terminal. This is what I desired.

Using the shell scipt, I can't get what I want (the redirection doesn't seem to work properly).

However, if I use a shell script, namely command.sh:

#! /bin/sh
nohup python print1.py &> 1.txt &
nohup python print2.py &> 2.txt &

And I type

$ sh command.sh

to run command.sh. The output is not what I expected. 1.txt and 2.txt will be created, but they contain nothing. A nohup.out file will also be created, in which number 1 and 2 are included.

And the output shown on the terminal is

$ nohup: appending output to 'nohup.out'
nohup: appending output to 'nohup.out'

This answer suggests that with #! /bin/sh, some shells may not understand &>. So I also tried changing #! /bin/sh to #! /bin/bash. However, it's also the same.

Can I get the same result using a shell script, just as I enter the commands separately?

  • If you explicitly type sh command.sh, you still have sh running the script. What happens with bash command.sh? Or if you make the script (with #!/bin/bash) executable and run ./command.sh?
    – DonHolgo
    Oct 14, 2022 at 15:10
  • @DonHolgo Thanks, it works! The only difference between these two methods and typing command directly is that there won't be a nohup: ignoring input line at the beginning of file 1.txt and 2.txt. This doesn't affect the output, right? BTW, if you add an answer, I will mark it as accepted.
    – user343233
    Oct 15, 2022 at 8:09

1 Answer 1


If you explicitly say sh command.sh, sh is used to run your script, regardless of the #! line. However, as the linked answer says, sh doesn't understand &> as an operator of its own and reads

nohup python print1.py &> 1.txt &

as two commands:

nohup python print1.py &
> 1.txt &

The first one causes the output of print1.py to be written to nohup.out, the second one creates an empty 1.txt.

If you want bash instead of sh to run your command.sh, you can either use

bash command.sh

or put #!/bin/bash instead of #!/bin/sh in the file, make it executable and then simply run


on its own.

  • Or use a compatible redirect: > 1.txt 2>&1 instead of the bash-only shorthand. Oct 16, 2022 at 18:46

You must log in to answer this question.

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