Saturday, June 29th, 2024
Shell scripts are the workhorses of the Linux and Unix world. They automate tasks, manage files, and orchestrate complex workflows. But let's face it, plain text output can get monotonous. This is where ANSI escape codes come in, adding a splash of color and functionality to your scripts, making them more informative, user-friendly, and even entertaining.
ANSI escape codes are a set of special byte sequences that control the terminal display. They are embedded within the text stream and interpreted by the terminal emulator, not displayed as characters themselves. These codes can achieve various effects, including:
Colored text grabs attention and improves comprehension. Imagine a script that identifies errors in red and successes in green. Suddenly, the output becomes self-explanatory, reducing the need for further analysis.
Colorful prompts can guide users through interactive scripts. For example, a script might display a bright yellow prompt for confirmation before deleting files.
ANSI escape codes can be used to create basic progress bars or spinning indicators, keeping users informed about long-running tasks.
By strategically positioning text and using separators, you can create custom layouts for displaying complex information.
ANSI escape codes are widely supported by most terminal emulators, making your scripts function consistently across different environments.
\033[0m
\033[1m
\033[2m
\033[3m
\033[4m
\033[5m
\033[7m
\033[8m
\033[9m
\033[30m
\033[31m
\033[32m
\033[33m
\033[34m
\033[35m
\033[36m
\033[37m
\033[39m
\033[40m
\033[41m
\033[42m
\033[43m
\033[44m
\033[45m
\033[46m
\033[47m
\033[49m
\033[90m
\033[91m
\033[92m
\033[93m
\033[94m
\033[95m
\033[96m
\033[97m
\033[100m
\033[101m
\033[102m
\033[103m
\033[104m
\033[105m
\033[106m
\033[107m
\033[38;5;{n}m
\033[48;5;{n}m
{n}
ranges from 0 to 255:
30-37
)90-97
)\033[38;2;{r};{g};{b}m
\033[48;2;{r};{g};{b}m
{r}
, {g}
, and {b}
are the red, green, and blue components (0-255).
\033[{n}A
(Moves the cursor up by n
lines)\033[{n}B
(Moves the cursor down by n
lines)\033[{n}C
(Moves the cursor right by n
columns)\033[{n}D
(Moves the cursor left by n
columns)\033[{row};{column}H
(Sets the cursor position to the specified row and column)\033[s
(Saves the current cursor position)\033[u
(Restores the cursor to the last saved position)\033[?25l
\033[?25h
\033[2J
(Clears the entire screen)\033[K
(Clears from the cursor to the end of the line)\033[1K
(Clears from the cursor to the start of the line)\033[0K
(Clears from the cursor to the end of the line)\033[?1049h
\033[?1049l
#!/bin/bash
# Function to print an error message in red
error_message() {
echo -e "\033[31mError: $1\033[0m"
}
# Example usage
if [ ! -f "myfile.txt" ]; then
error_message "File 'myfile.txt' does not exist!"
fi
#!/bin/bash
# Function to display a progress bar
draw_progress_bar() {
local progress=$(( $1 * 10 / $2 ))
local bar
# Save cursor position before drawing
echo -ne "\033[s"
for i in {1..10}; do
if [ $i -le $progress ]; then
bar="#";
else
bar=" ";
fi
echo -ne "$bar"
done
# Restore cursor position after drawing
echo -ne "\033[u"
}
# Simulate work with a loop
total_steps=10
draw_progress_bar 0 $total_steps
echo -e "Long running process:\033[?25l"
for i in $(seq 1 $total_steps); do
# Simulate work (replace with your actual command)
sleep 0.5
# Update progress bar for each step
draw_progress_bar $i $total_steps
done
echo -e "\nProcess completed.\033[?25h"
By incorporating ANSI escape codes, you can elevate your shell scripts from basic tools to user-friendly and informative experiences. So, unleash your inner script artist and start adding a splash of color to your shell scripting adventures!
Tags: shell scriptingcommand line
← The Little ThingsEmbracing Accessibility →