The Complete Guide to Bash Commands

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=$'nt'
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.



Source link
lol

By stp2y

Leave a Reply

Your email address will not be published. Required fields are marked *

No widgets found. Go to Widget page and add the widget in Offcanvas Sidebar Widget Area.