Home > other >  Shell script not sending output to file
Shell script not sending output to file

Time:12-11

Whenever I run the following command in the terminal, the stderr and stdout is sent to the output.txt file just like I would expect.

/usr/bin/time -v ls -al &> output.txt

However, whenever I put this command into a shell script, the output is displayed in the terminal and the output of output.txt is empty. Here is the equivalent script:

#!/bin/sh
/usr/bin/time -v ls -al &> output.txt
exit

I run the script using ./script.sh. Why doesn't this script behave like I would expect? I have tried replacing the command with: { /usr/bin/time -v ls -al ; } &> output.txt, but that doesn't work either.

Thanks in advance for any help!

CodePudding user response:

You are using sh as interpreter for your script, and the operator &> isn't supported by it. Surely you are using bash to test your command, and that is why it works outside the script.

I think that this answer to another question could be of some help: https://askubuntu.com/a/625230

  • Related