View the Bash Scripting eBook Web Page
mainThe eBook is also available as an interactive web page.
https://ebook.bobby.sh/repository·main·Indexed 27 days ago
https://github.com/bobbyiliev/introduction-to-bash-scriptingAn open-source educational resource and eBook for learning Bash scripting. It covers fundamental syntax—including variables, arguments, arrays, conditionals, loops, and functions—as well as practical automation tasks such as log parsing, working with JSON via jq, and interacting with the Cloudflare API.
The eBook is also available as an interactive web page.
https://ebook.bobby.sh/Bash (Bourne-Again SHell) is a Unix shell and command language used as the default command interpreter in most Linux systems. It can be used in two ways:
To write Bash scripts, you need a Unix terminal and a text editor (e.g., VS Code, Sublime Text, vim, or nano).
chmod +x. Then, execute the script by passing the path to your NGINX or Apache access log as the first argument. The script provides a summary including the top 20 pages with the most POST requests, the top 20 pages with the most GET requests, and the top 20 IP addresses with their geo-location.To improve the readability of your Bash scripts, you can define ANSI escape code variables and wrap them in helper functions to apply colors to text easily.
Define these at the start of your script:
green='\e[32m'blue='\e[34m'red='\e[31m'clear='\e[0m'Create functions that take a string as an argument and wrap it in the desired color codes:
ColorGreen(){
echo -ne "${green}${1}${clear}"
}
ColorBlue(){
echo -ne "${blue}${1}${clear}"
}Use command substitution to apply the color to a string:
echo -ne "$(ColorBlue 'Some text here')"ColorGreen(){
echo -ne "${green}${1}${clear}"
}
echo -ne "$(ColorGreen 'Some text here')"You can execute a local BASH script on multiple remote servers without manually copying the script to each machine or logging in individually. This is achieved by using a while loop to iterate through a list of server IP addresses and piping the local script into a remote bash -s command via SSH.
servers.txt containing one server IP address (or hostname) per line.remote_check.sh).while IFS= read -r server; do ssh "your_user@${server}" 'bash -s' < ./remote_check.sh ; done < servers.txtYou can create a shortcut for long or repetitive commands using the alias command. This shortcut exists only for the duration of your current terminal session. If you log out or close the terminal, the alias will be lost.
Syntax:
alias name="command"
alias conn="netstat -plant | grep '80\|443' | grep -v LISTEN | wc -l"Bash functions allow you to reuse code blocks. You can define them using two syntaxes: including the function keyword or omitting it. Using the function keyword is recommended for better readability.
Important: When calling a function, do not include parentheses ().
# Syntax 1: With 'function' keyword (Recommended)
function function_name() {
your_commands
}
# Syntax 2: Without 'function' keyword
function_name() {
your_commands
}
# Calling the function (Do NOT use parentheses here)
function_nameTo create a Bash script, create a file with a .sh extension. The first line of the script must contain the Shebang, which tells the operating system to execute the script using the /bin/bash interpreter.
#!/bin/bashThe case statement simplifies complex conditional logic when comparing a single variable against multiple patterns.
Syntax Rules:
case $variable in.).| operator.;;.* as a default pattern to catch anything that doesn't match previous cases.esac (case spelled backwards).Example of matching car brands to their manufacturing locations:
#!/bin/bash
read -p "Digite o nome da marca do seu carro: " carro
case $carro in
Tesla)
echo -n "A fábrica de carros de ${carro} está nos EUA."
;;
BMW | Mercedes | Audi | Porsche)
echo -n "A fábrica de carros de ${carro} fica na Alemanha."
;;
Toyota | Mazda | Mitsubishi | Subaru)
echo -n "A fábrica de carros de ${carro} está no Japão."
;;
*)
echo -n "${carro} é uma marca de carro desconhecida"
;;
esacYou can control the flow of loops using continue and break:
continue [n]: Skips the remainder of the current iteration and starts the next one. The optional argument [n] specifies which nesting level of loops to continue (e.g., continue 2 continues the second surrounding loop). continue 1 is equivalent to continue.break [n]: Terminates the loop immediately. The optional argument [n] specifies which nesting level of loops to exit. break 1 is equivalent to break. Use break 2 to exit a nested loop and the outer loop simultaneously.() when calling the function.