We shall fight on the beaches, we shall fight on the landing grounds, we shall fight in the fields and in the streets, we shall fight in the hills; we shall never surrender, Winston Churchill
#!/bin/bash
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
BLINK='\033[5m'
NC='\033[0m' # No Color
# Function for rainbow text
# Remember to use -e with echo to enable interpretation of backslash escapes
rainbow_echo() {
text=$1
for (( i=0; i<${#text}; i++ )); do
case $(($i % 6)) in
0) echo -en "${RED}${text:$i:1}" ;;
1) echo -en "${YELLOW}${text:$i:1}" ;;
2) echo -en "${GREEN}${text:$i:1}" ;;
3) echo -en "${CYAN}${text:$i:1}" ;;
4) echo -en "${BLUE}${text:$i:1}" ;;
5) echo -en "${PURPLE}${text:$i:1}" ;;
esac
done
# Always end colored text with ${NC} to reset the formatting
echo -e "${NC}"
}
# Function for typing effect
type_echo() {
text=$1
for (( i=0; i<${#text}; i++ )); do
echo -n "${text:$i:1}"
sleep 0.1
done
echo
}
# Different echo styles
echo -e "${BOLD}Bold Text${NC}"
echo -e "${UNDERLINE}Underlined Text${NC}"
echo -e "${BLINK}Blinking Text${NC}"
echo -e "${RED}Red Text${NC}"
echo -e "${BLUE}Blue${NC} and ${GREEN}Green${NC} Text"
echo -e "${YELLOW}${BOLD}Bold Yellow Text${NC}"
echo -e "${CYAN}${UNDERLINE}Underlined Cyan Text${NC}"
# Rainbow effect
rainbow_echo "Rainbow Text"
# Typing effect
type_echo "This text appears like typing..."
# Box style
echo "┌─────────────────┐"
echo "│ Boxed Message │"
echo "└─────────────────┘"
Let’s create two files: util.sh (definition and functions) and myscript.sh (it uses the functionality provided by util.sh).
#!/bin/sh
# This is util.sh
# Color codes
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
PURPLE='\033[0;35m'
CYAN='\033[0;36m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
BLINK='\033[5m'
NC='\033[0m' # No Color
# Define functions
# Function for rainbow text
rainbow_echo() {
text=$1
for (( i=0; i<${#text}; i++ )); do
case $(($i % 6)) in
0) echo -en "${RED}${text:$i:1}" ;;
1) echo -en "${YELLOW}${text:$i:1}" ;;
2) echo -en "${GREEN}${text:$i:1}" ;;
3) echo -en "${CYAN}${text:$i:1}" ;;
4) echo -en "${BLUE}${text:$i:1}" ;;
5) echo -en "${PURPLE}${text:$i:1}" ;;
esac
done
echo -e "${NC}"
}
# Function for typing effect
type_echo() {
text=$1
for (( i=0; i<${#text}; i++ )); do
echo -n "${text:$i:1}"
sleep 0.1
done
echo
}
# Echo with colors
print_color() {
local color=$1
local message=$2
echo -e "${color}${message}${NC}"
}
# A custom function to create boxed text
box_echo() {
local message="$1"
local padding=2 # Space on each side of the text
local message_length=${#message}
local box_width=$((message_length + (padding * 2)))
# Create top border with the correct width
local border=$(printf '─%.0s' $(seq 1 $box_width))
# Create padding for the message
local padding_spaces=$(printf ' %.0s' $(seq 1 $padding))
# Print the box with message
echo "╔${border}╗"
echo "║${padding_spaces}${message}${padding_spaces}║"
echo "╚${border}╝"
}
# Export function to make it available to other scripts
export -f print_color
export -f rainbow_echo
export -f type_echo
export -f box_echo
#!/bin/sh
# Import utils.sh
source ~/dotfiles/scripts/util.sh
rainbow_echo "I have yet to see any problem, however complicated, which, when looked at in the right way did not become still more complicated, Paul Anderson."
print_color "$RED" "This text is in red"
box_echo "This text is inside a box."
type_echo "This text appears like typing. Cool!"