287 lines
10 KiB
HTML
287 lines
10 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self'; font-src 'self' data:; img-src 'self' data:; connect-src 'self'; base-uri 'self'; form-action 'self' https://defcon.social https://bsky.app;">
|
|
<meta http-equiv="X-Content-Type-Options" content="nosniff">
|
|
<link rel="stylesheet" href="../assets/css/style.css">
|
|
<link rel="icon" type="image/x-icon" href="../favicon.ico">
|
|
<script>
|
|
// Apply theme immediately to prevent flash
|
|
(function() {
|
|
const theme = localStorage.getItem('theme') ||
|
|
(window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light');
|
|
document.documentElement.setAttribute('data-theme', theme);
|
|
})();
|
|
</script>
|
|
<title>Bash Quickstart Reference - Quick Reference - Launch Pad</title>
|
|
</head>
|
|
<body>
|
|
<button class="theme-toggle" id="themeToggle" aria-label="Toggle dark mode">
|
|
<svg class="theme-icon theme-icon-moon" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z"></path></svg>
|
|
<svg class="theme-icon theme-icon-sun" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" style="display: none;"><circle cx="12" cy="12" r="5"></circle><line x1="12" y1="1" x2="12" y2="3"></line><line x1="12" y1="21" x2="12" y2="23"></line><line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line><line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line><line x1="1" y1="12" x2="3" y2="12"></line><line x1="21" y1="12" x2="23" y2="12"></line><line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line><line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line></svg>
|
|
</button>
|
|
<br/><br/>
|
|
<div class="name">
|
|
__ _______________________ _________._________________________
|
|
/ \ / \_ _____/\______ \/ _____/| \__ ___/\_ _____/
|
|
\ \/\/ /| __)_ | | _/\_____ \ | | | | | __)_
|
|
\ / | \ | | \/ \| | | | | \
|
|
\__/\ / /_______ / |______ /_______ /|___| |____| /_______ /
|
|
\/ \/ \/ \/ \/
|
|
|
|
</div>
|
|
<div class="blog-page-header">
|
|
<div class="blog-header-content">
|
|
<a href="/quickref" class="back-link" title="Back to Quick Reference">
|
|
<svg xmlns="http://www.w3.org/2000/svg" width="42" height="42" viewBox="0 0 24 24" class="home-icon"><path fill="currentColor" d="M10 20v-6h4v6h5v-8h3L12 3 2 12h3v8z"/></svg>
|
|
</a>
|
|
<h1 class="blog-page-title">Bash Quickstart Reference</h1>
|
|
</div>
|
|
</div>
|
|
<div class="blog-post-container">
|
|
<div class="blog-posts-container" style="max-width: 900px; margin: 0 auto;">
|
|
<div class="blog-post">
|
|
<div class="blog-post-content">
|
|
<p><a href="index.html">← Back to quick reference</a></p>
|
|
<p><a href="../index.html">← Home</a></p>
|
|
<hr>
|
|
<p>Quick reference guide for Bash. Essential syntax, control flow, variables, arrays, and common patterns for shell scripting.</p>
|
|
<hr>
|
|
<h2>Hello World</h2>
|
|
<pre><code>#!/bin/bash
|
|
echo "Hello, World!" # The shebang tells the system which interpreter to use</code></pre>
|
|
<hr>
|
|
<h2>Variables</h2>
|
|
<pre><code>name="Alice" # No spaces around =; quotes protect spaces
|
|
x=42 # Numbers are strings until you do math
|
|
readonly PI=3.14 # Make variable read-only
|
|
unset name # Delete variable
|
|
|
|
echo $name # Access with $ prefix
|
|
echo ${name} # Braces are safer for variable expansion
|
|
echo "${name}_suffix" # Use braces when concatenating</code></pre>
|
|
<hr>
|
|
<h2>Control Flow</h2>
|
|
<h3>If/Else</h3>
|
|
<pre><code>if [ $x -gt 10 ]; then # [ ] requires spaces; -gt means "greater than"
|
|
echo "x is big"
|
|
else
|
|
echo "x is small"
|
|
fi
|
|
|
|
# Comparison operators: -eq, -ne, -lt, -le, -gt, -ge
|
|
# String comparison: =, !=, -z (empty), -n (non-empty)
|
|
|
|
if [ -f "file.txt" ]; then # -f checks if file exists
|
|
echo "File exists"
|
|
elif [ -d "dir" ]; then # -d checks if directory exists
|
|
echo "Directory exists"
|
|
fi</code></pre>
|
|
<h3>For Loops</h3>
|
|
<pre><code>for i in 1 2 3 4 5; do
|
|
echo $i
|
|
done
|
|
|
|
for i in {1..5}; do # Brace expansion: 1 to 5
|
|
echo $i
|
|
done
|
|
|
|
for file in *.txt; do # Iterate over files
|
|
echo "Processing $file"
|
|
done
|
|
|
|
for ((i=0; i<5; i++)); do # C-style for loop
|
|
echo $i
|
|
done</code></pre>
|
|
<h3>While Loops</h3>
|
|
<pre><code>while [ $x -gt 0 ]; do
|
|
echo $x
|
|
x=$((x - 1)) # Arithmetic expansion
|
|
done
|
|
|
|
# Read file line by line
|
|
while IFS= read -r line; do
|
|
echo "$line"
|
|
done < file.txt</code></pre>
|
|
<h3>Case Statement</h3>
|
|
<pre><code>case $choice in
|
|
"1")
|
|
echo "Option 1"
|
|
;;
|
|
"2"|"two")
|
|
echo "Option 2"
|
|
;;
|
|
*)
|
|
echo "Default case"
|
|
;;
|
|
esac</code></pre>
|
|
<hr>
|
|
<h2>Functions</h2>
|
|
<pre><code>function greet() {
|
|
local name=$1 # $1 is first argument; local limits scope
|
|
echo "Hello, $name!"
|
|
}
|
|
|
|
greet "Alice" # Call function
|
|
|
|
# Alternative syntax
|
|
add() {
|
|
local a=$1
|
|
local b=$2
|
|
return $((a + b)) # Return exit code (0-255)
|
|
}
|
|
|
|
add 2 3
|
|
result=$? # $? is exit code of last command</code></pre>
|
|
<hr>
|
|
<h2>Arrays</h2>
|
|
<pre><code>fruits=("apple" "banana" "cherry") # Declare array
|
|
fruits[3]="date" # Add element
|
|
echo ${fruits[0]} # Access element (apple)
|
|
echo ${fruits[@]} # All elements
|
|
echo ${#fruits[@]} # Array length
|
|
|
|
for fruit in "${fruits[@]}"; do # Iterate over array
|
|
echo $fruit
|
|
done</code></pre>
|
|
<hr>
|
|
<h2>Strings</h2>
|
|
<pre><code>text="Hello, World!"
|
|
echo ${#text} # String length
|
|
echo ${text:0:5} # Substring: start at 0, length 5 ("Hello")
|
|
echo ${text/World/Bash} # Replace first match ("Hello, Bash!")
|
|
echo ${text//l/L} # Replace all matches
|
|
echo ${text#Hello, } # Remove shortest prefix match
|
|
echo ${text##Hello, } # Remove longest prefix match
|
|
echo ${text% World!} # Remove shortest suffix match
|
|
echo ${text%% World!} # Remove longest suffix match</code></pre>
|
|
<hr>
|
|
<h2>Input / Output</h2>
|
|
<pre><code>read name # Read user input
|
|
echo "Enter your name: "
|
|
read -p "Name: " name # Prompt and read in one line
|
|
read -s password # Silent input (for passwords)
|
|
read -a array # Read into array
|
|
|
|
echo "Hello" # Print to stdout
|
|
echo -n "No newline" # -n suppresses newline
|
|
printf "Value: %d\n" 42 # Formatted output</code></pre>
|
|
<hr>
|
|
<h2>Command Substitution</h2>
|
|
<pre><code>date=$(date) # Capture command output (modern syntax)
|
|
date=`date` # Backticks (older syntax, less preferred)
|
|
files=$(ls *.txt) # List of files
|
|
count=$(wc -l < file.txt) # Line count
|
|
|
|
# Process substitution
|
|
diff <(sort file1.txt) <(sort file2.txt)</code></pre>
|
|
<hr>
|
|
<h2>File Operations</h2>
|
|
<pre><code># File tests
|
|
[ -f "file.txt" ] # File exists and is regular file
|
|
[ -d "dir" ] # Directory exists
|
|
[ -r "file.txt" ] # File is readable
|
|
[ -w "file.txt" ] # File is writable
|
|
[ -x "file.txt" ] # File is executable
|
|
[ -s "file.txt" ] # File exists and is not empty
|
|
|
|
# Reading files
|
|
while IFS= read -r line; do
|
|
echo "$line"
|
|
done < file.txt
|
|
|
|
# Writing files
|
|
echo "content" > file.txt # Overwrite
|
|
echo "content" >> file.txt # Append</code></pre>
|
|
<hr>
|
|
<h2>Arithmetic</h2>
|
|
<pre><code>x=10
|
|
y=5
|
|
|
|
# Arithmetic expansion
|
|
result=$((x + y)) # Addition
|
|
result=$((x - y)) # Subtraction
|
|
result=$((x * y)) # Multiplication
|
|
result=$((x / y)) # Division
|
|
result=$((x % y)) # Modulo
|
|
result=$((x ** 2)) # Exponentiation
|
|
|
|
# Increment/decrement
|
|
((x++)) # Post-increment
|
|
((++x)) # Pre-increment
|
|
((x--)) # Decrement</code></pre>
|
|
<hr>
|
|
<h2>Error Handling</h2>
|
|
<pre><code>set -e # Exit on error
|
|
set -u # Exit on undefined variable
|
|
set -o pipefail # Exit on pipe failure
|
|
|
|
# Trap errors
|
|
trap 'echo "Error occurred"' ERR
|
|
|
|
# Check command success
|
|
if command; then
|
|
echo "Success"
|
|
else
|
|
echo "Failed"
|
|
fi
|
|
|
|
# Check exit code
|
|
command
|
|
if [ $? -eq 0 ]; then
|
|
echo "Success"
|
|
fi</code></pre>
|
|
<hr>
|
|
<h2>Tips</h2>
|
|
<ul>
|
|
<li>Always quote variables to prevent word splitting: <code>"$var"</code> not <code>$var</code>.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>[[ ]]</code> instead of <code>[ ]</code> for more features and fewer surprises (Bash 4+).</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>local</code> in functions to avoid polluting global scope.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Test file existence with <code>-f</code> (file) or <code>-d</code> (directory) before operations.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>$(( ))</code> for arithmetic; it's safer than <code>expr</code>.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Prefer <code>$(command)</code> over backticks for command substitution.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>read -r</code> to prevent backslash interpretation when reading lines.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Set <code>IFS=</code> when reading to preserve leading/trailing whitespace.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>set -e</code> at the start of scripts to exit on errors (unless you handle them explicitly).</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Always start scripts with <code>#!/bin/bash</code> (or appropriate shebang) for portability.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Use <code>${var:-default}</code> to provide default values: <code>name=${1:-"World"}</code>.</li>
|
|
</ul>
|
|
<ul>
|
|
<li>Check if a command exists before using it:
|
|
<pre><code>if command -v git &> /dev/null; then
|
|
git --version
|
|
fi</code></pre>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<script async type="text/javascript" src="../blog/analytics.js"></script>
|
|
<script src="../theme.js"></script>
|
|
</body>
|
|
</html>
|
|
|