avatarSkilled Coder

Summary

The web content provides advanced Bash scripting techniques for power users, showcasing unique features of the Bash shell to enhance command-line efficiency and scripting capabilities.

Abstract

The article "Unique and Lesser-Known Bash Script Tricks" introduces a collection of sophisticated Bash commands and features tailored for advanced users. It covers process substitution with named pipes, network communication via /dev/tcp and /dev/udp, recursive file searching with globstar, string manipulation with parameter expansion, extended pattern matching with extglob, arithmetic evaluations, anonymous functions, associative arrays, indirect variable expansion, and secure temporary file creation with mktemp. These features are demonstrated with practical examples, emphasizing their utility in streamlining scripting and command-line tasks without relying on external tools. The article encourages readers to apply these techniques in Unix-like operating systems, such as Linux and macOS, and provides guidance for Windows users through alternative shell environments.

Opinions

  • The author believes that mastering these Bash tricks can significantly improve the user's command-line experience and scripting prowess.
  • There is an emphasis on the practicality of using Bash's built-in features to perform tasks more efficiently.
  • The article suggests that using these advanced Bash functionalities can lead to more elegant and powerful scripting.
  • The author implies that these techniques are underutilized, hence the term "lesser-known."
  • By sharing these tricks on social media platforms like Twitter and Instagram, the author indicates a commitment to community engagement and continuous learning within the programming community.

Unique and Lesser-Known Bash Script Tricks

Command Line Hacks for Power Users

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)

On my Twitter and Instagram accounts, I frequently share my programming journey and development experiences.

Follow and subscribe to emails for unique coding articles.

Keep exploring, keep learning, and keep coding!

Thanks for reading :)

Bash
Linux
Coding
Command Line
Shell Script
Recommended from ReadMedium