$log_file 2>&1 # Update list o" /> $log_file 2>&1 # Update list o" /> $log_file 2>&1 # Update list o"/>

Redirect all output of a bash script, including commands

61 Views Asked by At

I'm learning to write bash scripts and I have a script that goes like this:

#!/bin/bash
log_file="user_data.log"
exec > $log_file 2>&1

# Update list of available packages
sudo apt update

# Install required packages
sudo apt install docker.io docker-compose git nginx certbot python3-certbot-nginx -y 

I get stdin and stderr redirected to user_data.log, however i would like to see commands (e.g. sudo apt update) in the same logfile. How do I do this? Is there a better way to achieving this (without sh script.sh > somewhere)? thank you so much for your help.

1

There are 1 best solutions below

0
Alexey Kiyashkin On

As commented by @KamilCuk, I needed to add this at the start of the script:

set -x