Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Shell script not sending output to file

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thanks in advance for any help!

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading