Linux terminal output redirection

I want to redirect the output of a bash script file to a file.

script:

#!/bin/bash
echo "recursive c"
for ((i=0;i<=20;i+=1)); do
time ./recursive
done

But if I ran it like this:

script.sh >> temp.txt

only the output will be written in the file. / recursive.

I want to capture the output of a time command in a file.

+3
source share
2 answers

Redirect STDERRto STDOUT:

script.sh >> temp.txt  2>&1

Or if bash4.0 is used :

$ script.sh &>> temp.txt

(Thanks for the second form, go to the comment. I cannot verify since I have the previous one bash.)


My tests were awesome:

$ time sleep 1 > /dev/null 2>&1

real    0m1.036s
user    0m0.002s
sys     0m0.032s

The problem is that redirection was included as part of the synchronization command. Here is the solution for this test:

$ (time sleep 1) > /dev/null 2>&1

, , , , .

+8

& → , :

$ script.sh 2>&1 |tee -a temp.txt
+2

Source: https://habr.com/ru/post/1703847/


All Articles