I am running the following git clone command through sudo and bash and I want to redirect STDOUT to a log file:% sudo -u test_user bash -c git clone https://github.com/scrooloose/nerdtree.git/home/test_user/.vim/bundle/nerdtree >> /var/log/build_scripts.logWhat is happening is the STDOUT is continuing to be sent to the terminal. i.e.Cloning into 'nerdtree'...remote: Counting objects: 3689, done.[...]Checking connectivity... done.I'm guessing the problem has something to do with the fact that sudo is forking a new process then bash is forking another, as demonstrated here:% sudo -u test_user bash -c { git clone https://github.com/scrooloose/nerdtree.git/home/test_user/.vim/bundle/nerdtree >> /var/log/build_scripts.log; ps f -g$$; }    PID TTY      STAT   TIME COMMAND 6556 pts/25   Ss     0:02 /usr/bin/zsh 3005 pts/25   S+     0:00  \_ sudo -u test_user bash -c { git clone https://github.com/scrooloo 3006 pts/25   S+     0:00      \_ bash -c { git clone https://github.com/scrooloose/nerdtree. 3009 pts/25   R+     0:00          \_ ps f -g6556I've triedrunning this in a script and using exec >> /var/log/build_script.log before the commandwrapping the command in a function, then calling and redirecting the functions outputBut I think these redirections are only applying to the parent and  the child processes are defaulting to sending STDOUT to the /dev/tty/25 of their parent causing output to continue to the terminal.How can I redirect the STDOUT of this command?
How can I redirect the output of a child process?
shell script;io redirection
The messages you mention are not printed to standard output but to standard error. So, to capture them, you either need to redirect standard error instead of standard output:sudo -u user bash -c git clone https://github.com/foo.git ~/foo 2>> logOr both STDERR and STDOUT:sudo -u user bash -c git clone https://github.com/foo.git ~/foo >> log 2>&1With bash, you can also use &>> for this:sudo -u user bash -c git clone https://github.com/foo.git ~/foo &>> logThe csh, tcsh, zsh equivalent being >>& ((t)csh don't support 2>&1 so it's the only way there):sudo -u user csh -c git clone https://github.com/foo.git ~/foo >>& logIn fishsudo -u user fish -c git clone https://github.com/foo.git ~/foo >> log ^&1For more on the different types of redirection operators, see What are the shell's control and redirection operators?Now, in the specific case of git, there's another issue. Like a few other programs, git can detect that its output is being redirected and stops printing progress reports if so. This is probably because the reports are intended to be seen live and include \r which can be a problem when saved in a file. To get around this, use:       --progress       Progress status is reported on the standard error stream by default       when it is attached to a terminal, unless -q is specified. This       flag forces progress status even if the standard error stream is       not directed to a terminal.And:sudo -u user bash -c git clone --progress https://github.com/foo.git ~/foo >> log 2>&1If you want to both see the output as it comes and save to a file, use tee:sudo -u user bash -c git clone --progress https://github.com/foo.git ~/foo 2>&1 |     tee -a log