You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
π Bash Command Chaining Mastery: The Ultimate Cheat Sheet
π Basic Command Chaining
Operator
Description
Example
;
π Run commands sequentially
echo "Hello"; echo "World"
&&
β Run next command only if previous succeeds
mkdir dir && cd dir
||
β Run next command only if previous fails
ping -c1 google.com || echo "Offline"
|
πΏ Pipe output of one command to another
ls -l | grep ".txt"
π Command Grouping
Syntax
Description
Example
( ... )
π£ Run commands in a subshell
(cd /tmp && ls) | grep log
{ ...; }
π¦ Run commands in current shell
{ echo "Start"; ls; echo "End"; }
π Loop Constructs
Syntax
Description
Example
for
π Iterate over a list
for i in {1..5}; do echo $i; done
while
π Run while condition is true
while true; do date; sleep 1; done
until
π Run until condition is true
until ping -c1 google.com; do sleep 5; done
π Conditional Execution
Syntax
Description
Example
if...then...fi
π Conditional execution
if [ -f file.txt ]; then echo "Exists"; fi
case...esac
π’ Multi-way decision
case $var in 1) echo "One";; 2) echo "Two";; esac
π£ Input/Output Redirection
Operator
Description
Example
>
π€ Redirect stdout to file (overwrite)
echo "Hello" > file.txt
>>
π₯ Redirect stdout to file (append)
echo "World" >> file.txt
<
π₯ Redirect file to stdin
sort < unsorted.txt
2>
π¨ Redirect stderr to file
gcc program.c 2> errors.log
&>
π Redirect both stdout and stderr
command &> output.log
π Advanced Chaining Techniques
Technique
Description
Example
xargs
π¨ Build command lines from standard input
find . -name "*.txt" | xargs grep "pattern"
tee
π± Send output to both file and stdout
echo "log" | tee -a logfile.txt
command substitution
π Use output of a command as argument
echo "Today is $(date +%A)"
process substitution
π Use output of command as file
diff <(sort file1) <(sort file2)
ποΈ Job Control
Command
Description
Example
&
πββοΈ Run command in background
long_running_command &
jobs
π List background jobs
jobs
fg
βΆοΈ Bring job to foreground
fg %1
bg
πββοΈ Resume job in background
bg %2
wait
β³ Wait for background jobs to finish
wait %1
π Pro Tips
π Use set -e at the start of your script to exit on any command failure
π Use set -x for debugging to print each command before execution
π’ Use $? to check the exit status of the last command
π Combine && and \|\| for if-else like behavior: command && success_command \|\| failure_command
π Use while read to process file contents line by line
π₯ Use <<EOF for here-documents to pass multi-line input to a command
π Use set -o noclobber to prevent accidental file overwrites with >
π£ Remember that 2>&1 redirects stderr to stdout
π¨ Use trap to set up commands that run on script exit or on signals
π Use time command to measure execution time of commands or scripts
π Example: Combining Multiple Techniques
#!/bin/bashset -e
# Function to process filesprocess_file() {
echo"Processing $1"# Simulating processing with sleep
sleep 1
}
# Find all .txt files and process them in parallel
find . -name "*.txt"| xargs -I {} -P 4 bash -c 'process_file "$@"' _ {}
# Wait for all background jobs to finishwait# Check if any errors occurredif [ $?-eq 0 ];thenecho"All files processed successfully"| tee success.log
elseecho"Some errors occurred during processing"| tee error.log >&2fi# Cleanup temporary files
rm -f *.tmp &&echo"Cleanup completed"||echo"Cleanup failed"
Remember, with great power comes great responsibility. Use these techniques wisely to create efficient and readable Bash scripts! ππ»