Some unique and lesser-known Bash tricks that can enhance your scripting and command-line experience.
So open your terminal and type these commands.
(The Bash tricks I shared are specific to the Bash shell, which is commonly used in Unix-like operating systems such as Linux and macOS)
Process Substitution with Named Pipes (<(...)
and >(...)
)
Process substitution allows you to use the output of a command as a temporary file. This is particularly useful when a command requires a filename as an argument.
diff <(sort file1.txt) <(sort file2.txt)
This compares the sorted versions of two files without creating intermediate files.
Using /dev/tcp
and /dev/udp
for Network Communications
Bash can perform TCP and UDP network operations without external utilities.
# Check if a port is open
timeout 1 bash -c "</dev/tcp/google.com/80" && echo "Open" || echo "Closed"
This attempts to open a TCP connection to google.com
on port 80
.
Recursive Globbing with globstar
(**
)
By enabling the globstar
option, you can use **
to match directories recursively.
shopt -s globstar
ls **/*.txt
This lists all .txt
files in the current directory and all subdirectories.
String Manipulation with Parameter Expansion
Bash provides powerful string manipulation without invoking external commands.
FILENAME="archive.tar.gz"
echo "${FILENAME%.tar.gz}" # Output: archive
This removes the .tar.gz
suffix from the filename.
Extended Pattern Matching with extglob
Enable extglob
to use advanced pattern matching in your scripts.
shopt -s extglob
rm !(*.txt)
This command deletes all files except those ending with .txt
.
Arithmetic Evaluation with let
, $((...))
, and ((...))
Perform arithmetic operations without external tools.
COUNT=0
let COUNT++
echo $COUNT # Output: 1
# Or using ((...))
((COUNT+=5))
echo $COUNT # Output: 6
Creating Anonymous Functions
Define functions without names for quick, one-time use.
(function(){ echo "Hello from an anonymous function!"; })
This executes the function immediately without polluting the namespace.
Associative Arrays for Key-Value Storage
Bash 4+ supports associative arrays, allowing you to map strings to values.
declare -A fruits
fruits[apple]="red"
fruits[banana]="yellow"
echo "${fruits[apple]}" # Output: red
Dynamic Variable Names with Indirect Expansion
Use the value of a variable as the name of another variable.
VAR_NAME="USER"
echo "${!VAR_NAME}" # Equivalent to echo "$USER"
Temporary Files with mktemp
Securely create temporary files or directories in your scripts.
TMPFILE=$(mktemp)
echo "Data" > "$TMPFILE"
# Do something with $TMPFILE
rm "$TMPFILE"
This avoids race conditions and security issues associated with predictable filenames.
These tricks leverage built-in Bash features to perform tasks more efficiently and elegantly. They can help you write more powerful scripts and use the command line more effectively.
You can directly run it on Linux and macOS.
You have a few options if you’re using Windows:
Windows Subsystem for Linux
Git Bash
Cygwin
MSYS2
PowerShell (native Windows scripting capabilities)