ubuntu-update.sh
ubuntu-update.sh copied to clipboard
Only use colors in a TTY
Summary
Right now, the script always uses colorized output. This is a nice user experience when run interactively in the terminal, but isn't so nice when printed to a log file and those pretty colors become ugly escape sequences. This PR preserves the colors when output is connected to a terminal, but removes them when output is piped somewhere else. The behavior is similar to tools like ls
and grep --color=auto
.
Manual Testing
Tested on Ubuntu 22.04.4 LTS.
Current Behavior
When running sudo ./ubuntu-update.sh
in a terminal, we see pretty colors:
But when running sudo ./ubuntu-update.sh > out-before.txt
, we get these file contents:
^[[31m
ubuntu-update.sh Copyright (C) 2018 Ted LeRoy
Easily update, upgrade, and clean up your Ubuntu system with this bash script.
This program comes with ABSOLUTELY NO WARRANTY see
https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
This is free software, and you are welcome to redistribute it
under certain conditions.
See https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
for details.^[(B^[[0m
^[[32m#############################
# Updating Data Base #
#############################^[[0m
...
New Behavior
When running sudo ./ubuntu-update.sh
in a terminal, we still see pretty colors:
And when running sudo ./ubuntu-update.sh > out-before.txt
, we get these nicer, un-colored file contents:
ubuntu-update.sh Copyright (C) 2018 Ted LeRoy
Easily update, upgrade, and clean up your Ubuntu system with this bash script.
This program comes with ABSOLUTELY NO WARRANTY see
https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
This is free software, and you are welcome to redistribute it
under certain conditions.
See https://github.com/TedLeRoy/ubuntu-update.sh/blob/master/LICENSE.md
for details.
#############################
# Updating Data Base #
#############################
...