Logo

dev-resources.site

for different kinds of informations.

The Complete Guide to Bash Commands

Published at
1/9/2025
Categories
bash
linux
automation
shell
Author
nolunchbreaks_22
Categories
4 categories in total
bash
open
linux
open
automation
open
shell
open
Author
16 person written this
nolunchbreaks_22
open
The Complete Guide to Bash Commands

Basic Syntax and Variables

Variables

name="John"
echo $name    # Basic output
echo "$name"  # Recommended way (with quotes)
echo "${name}"  # Most explicit way
Enter fullscreen mode Exit fullscreen mode

String Quotes

  • Double quotes allow variable expansion: "Hello $name"
  • Single quotes preserve literal text: 'Hello $name'

Shell Execution

echo "Current directory: $(pwd)"   # Modern syntax
echo "Current directory: `pwd`"    # Legacy syntax
Enter fullscreen mode Exit fullscreen mode

Control Flow

Conditional Execution

git commit && git push             # Run second command only if first succeeds
git commit || echo "Commit failed" # Run second command only if first fails
Enter fullscreen mode Exit fullscreen mode

Functions

get_name() {
    echo "John"
}
echo "You are $(get_name)"
Enter fullscreen mode Exit fullscreen mode

Conditionals

if [[ -z "$string" ]]; then
    echo "String is empty"
elif [[ -n "$string" ]]; then
    echo "String is not empty"
fi
Enter fullscreen mode Exit fullscreen mode

Parameter Expansions

Basic Operations

name="John"
echo "${name}"           # Standard output
echo "${name/J/j}"      # Substitution (john)
echo "${name:0:2}"      # Slicing (Jo)
echo "${name::2}"       # Slicing from start (Jo)
echo "${name::-1}"      # Slice except last char (Joh)
echo "${name:(-1)}"     # Last character (n)
echo "${food:-Cake}"    # Default value if unset
Enter fullscreen mode Exit fullscreen mode

Advanced Expansions

str="/path/to/foo.cpp"
echo "${str%.cpp}"      # Remove .cpp
echo "${str%.cpp}.o"    # Replace with .o
echo "${str%/*}"        # Remove last segment
echo "${str##*.}"       # Get extension
echo "${str##*/}"       # Get filename
echo "${str#*/}"        # Remove first segment
echo "${str/foo/bar}"   # Replace foo with bar
Enter fullscreen mode Exit fullscreen mode

Arrays

Array Operations

# Definition
Fruits=('Apple' 'Banana' 'Orange')

# Access
echo "${Fruits[0]}"     # First element
echo "${Fruits[-1]}"    # Last element
echo "${Fruits[@]}"     # All elements
echo "${#Fruits[@]}"    # Number of elements
echo "${#Fruits}"       # Length of first element

# Modification
Fruits+=('Watermelon')  # Add element
unset Fruits[2]         # Remove element
Fruits=("${Fruits[@]}") # Recreate array
Enter fullscreen mode Exit fullscreen mode

Dictionaries (Associative Arrays)

# Declaration
declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"

# Access
echo "${sounds[dog]}"   # Single value
echo "${sounds[@]}"     # All values
echo "${!sounds[@]}"    # All keys
echo "${#sounds[@]}"    # Number of elements
Enter fullscreen mode Exit fullscreen mode

File Conditions

[[ -e FILE ]]    # Exists
[[ -r FILE ]]    # Readable
[[ -h FILE ]]    # Symlink
[[ -d FILE ]]    # Directory
[[ -w FILE ]]    # Writable
[[ -s FILE ]]    # Size > 0 bytes
[[ -f FILE ]]    # Regular file
[[ -x FILE ]]    # Executable
Enter fullscreen mode Exit fullscreen mode

String Conditions

[[ -z STRING ]]          # Empty string
[[ -n STRING ]]          # Not empty string
[[ STRING == STRING ]]   # Equal
[[ STRING != STRING ]]   # Not equal
[[ STRING =~ REGEX ]]    # Regex match
Enter fullscreen mode Exit fullscreen mode

Numeric Conditions

[[ NUM -eq NUM ]]   # Equal
[[ NUM -ne NUM ]]   # Not equal
[[ NUM -lt NUM ]]   # Less than
[[ NUM -le NUM ]]   # Less than or equal
[[ NUM -gt NUM ]]   # Greater than
[[ NUM -ge NUM ]]   # Greater than or equal
Enter fullscreen mode Exit fullscreen mode

Loops

For Loops

# Basic for loop
for i in /etc/rc.*; do
    echo "$i"
done

# C-style for loop
for ((i = 0; i < 100; i++)); do
    echo "$i"
done

# Range loop
for i in {1..5}; do
    echo "Welcome $i"
done

# With step size
for i in {5..50..5}; do
    echo "Welcome $i"
done
Enter fullscreen mode Exit fullscreen mode

While Loops

# Reading lines
while read -r line; do
    echo "$line"
done <file.txt

# Infinite loop
while true; do
    echo "Forever"
done
Enter fullscreen mode Exit fullscreen mode

Input/Output

Redirection

command > output.txt      # stdout to file
command >> output.txt     # stdout to file (append)
command 2> error.log      # stderr to file
command 2>&1             # stderr to stdout
command &>/dev/null      # stdout and stderr to null
Enter fullscreen mode Exit fullscreen mode

Reading Input

echo -n "Proceed? [y/n]: "
read -r ans
echo "$ans"

read -n 1 ans    # Read single character
Enter fullscreen mode Exit fullscreen mode

Special Variables

$?      # Exit status of last command
$!      # PID of last background process
$$      # Current shell PID
$0      # Script name
$_      # Last argument of previous command
$#      # Number of arguments
$@      # All arguments as separate words
$*      # All arguments as a single word
Enter fullscreen mode Exit fullscreen mode

Debugging and Error Handling

Strict Mode

set -euo pipefail
IFS=$'\n\t'
Enter fullscreen mode Exit fullscreen mode

Trap Errors

trap 'echo Error at about $LINENO' ERR

# Or with function
traperr() {
    echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}
set -o errtrace
trap traperr ERR
Enter fullscreen mode Exit fullscreen mode

Advanced Features

Case Statements

case "$1" in
    start|up)
        vagrant up
        ;;
    stop|down)
        vagrant halt
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        ;;
esac
Enter fullscreen mode Exit fullscreen mode

Here Documents

cat <<END
hello world
END
Enter fullscreen mode Exit fullscreen mode

Command Substitution

result=$(command)
lines=(`cat "logfile"`)
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Always quote variables unless you specifically need word splitting
  2. Use set -e to make scripts exit on error
  3. Use meaningful variable names
  4. Comment your code
  5. Use functions for repeated operations
  6. Check for required commands at script start
  7. Provide usage information for scripts
  8. Use shellcheck to validate your scripts

Remember that Bash is both powerful and complex. These commands and constructs can be combined in numerous ways to create sophisticated scripts. Always test your scripts thoroughly, especially when dealing with file operations or system commands.

bash Article's
30 articles in total
Favicon
Understanding Linux Shells: Interactive, Non-Interactive, and RC Files
Favicon
Fixing Linux Backup Sync Issues for exFAT Compatibility
Favicon
Ergonomic Pyhon Text Piping Solution for Linux Shell with pypyp and uv
Favicon
Changing the Login Shell with Security and Interactivity
Favicon
Final Bash Script Series Mastering Remote Server Management and Web App Deployment
Favicon
Writting a GH extension with AI
Favicon
Test GPIO pins on BeagleBone Black by toggling high to low
Favicon
NYP Infosec December CTF 2024 writeup!
Favicon
Building a Smart Heater Controller with Python, Docker, and Bluetooth #3
Favicon
The Complete Guide to Bash Commands
Favicon
How to Get Started with Bash Scripting for Automation
Favicon
Building a Basic Testing Framework in Bash 🐚
Favicon
Funny how, as I was writing this, I started wondering—could we make this pipeline even smarter? That’s when SonarQube came to mind. But can it even work with Bash scripts? I’d love to hear your thoughts!
Favicon
Domina Bash con ejemplos prácticos de Git
Favicon
Explaining Null Device or Black Hole Register in Vim
Favicon
A Media Server on Steroids - Walkthrough
Favicon
From FZF file preview to a browser for cht.sh to discovering the ideal solution
Favicon
Code. Battery notifier.
Favicon
🌟Simplifying User Account Management with Bash Scripting Day3🌟
Favicon
Become a Bash Scripting Pro in 10 Minutes: A Quick Guide for Beginners
Favicon
Automatically Monitor and Manage RAM Usage for Next.js Development
Favicon
Bash Script - Task Management
Favicon
Bash Your Tools
Favicon
Bash Script Series: Automating Log Analysis with Bash Script or Shell Script
Favicon
The Most Interesting Mostly Tech Reads of 2024
Favicon
🚀 Automating Process Monitoring & Restarting with Bash Scripting Day4! 🔧
Favicon
🚀 RazzShell v1.0.1 is Here: Plugin Support, Enhanced Job Management, and More! 🌟
Favicon
Step-by-Step Guide: Assigning a Namecheap Domain to DigitalOcean Hosting with Nginx
Favicon
Simplify GitHub Workflow Management with My-GitHub-Manager
Favicon
How to play a bunch of lemmings

Featured ones: