Fundamentals 6 min read

Master Linux Parameter Expansion: Powerful Bash Tricks for Efficient Scripting

This guide explains the core concepts of Bash parameter expansion, walks through default values, conditional replacements, substring operations, deletions, and string substitutions, and demonstrates real‑world scripts for batch renaming, path handling, and simplifying user input.

Ops Development & AI Practice
Ops Development & AI Practice
Ops Development & AI Practice
Master Linux Parameter Expansion: Powerful Bash Tricks for Efficient Scripting

Linux command‑line power relies heavily on parameter expansion, a set of Bash syntaxes that transform variable values for complex string handling. Understanding these expansions lets you write shorter, more robust shell scripts.

1. Basic Concept

In shell scripts, parameter expansion applies special syntax to a variable’s value, producing a new string or altering the original. It is commonly used for manipulating strings and file paths, reducing script length and improving readability.

2. Common Parameter Expansion Types

Default Values and Assignment

${parameter:-word}

: Use word as a default when the variable is unset or empty. ${parameter:=word}: Assign word to the variable if it is unset or empty, then return the value.

#!/bin/bash
name=${1:-"World"}
echo "Hello, $name!"

user=${USER_NAME:="default_user"}
echo "Current user: $user"

Conditional Replacement

${parameter:+word}

: Return word if the variable is set and non‑empty; otherwise return an empty string. ${parameter:?word}: If the variable is unset or empty, print word to standard error and abort the script.

#!/bin/bash
greeting=${name:+"Hi there!"}
echo ${greeting:="Hello, stranger!"}

echo "The script will now exit if the USER_NAME variable is not set."
echo ${USER_NAME:?"USER_NAME is not set"}

Substring Extraction

${parameter:offset}

: Extract the substring starting at offset. ${parameter:offset:length}: Extract length characters starting at offset.

#!/bin/bash
text="Hello, Linux!"
echo ${text:7}      # outputs "Linux!"
echo ${text:7:5}    # outputs "Linux"

Substring Deletion

${parameter#word}

: Remove the shortest matching word pattern from the beginning. ${parameter##word}: Remove the longest matching word pattern from the beginning. ${parameter%word}: Remove the shortest matching word pattern from the end. ${parameter%%word}: Remove the longest matching word pattern from the end.

#!/bin/bash
file="/usr/local/bin/script.sh"
echo ${file#*/}   # outputs "usr/local/bin/script.sh"
echo ${file##*/}  # outputs "script.sh"
echo ${file%/*}   # outputs "/usr/local/bin"
echo ${file%%/*}  # outputs ""

String Replacement

${parameter/pattern/replacement}

: Replace the first occurrence of pattern with replacement. ${parameter//pattern/replacement}: Replace all occurrences of pattern with replacement.

#!/bin/bash
message="Hello, world! Hello, Linux!"
echo ${message/Hello/Hi}        # outputs "Hi, world! Hello, Linux!"
echo ${message//Hello/Hi}       # outputs "Hi, world! Hi, Linux!"

3. Practical Use Cases

Batch Renaming Files

#!/bin/bash
for file in *.txt; do
    mv "$file" "${file%.txt}.bak"
done

Path String Manipulation

#!/bin/bash
path="/home/user/docs/report.txt"
dirname=${path%/*}
basename=${path##*/}
extension=${path##*.}

echo "Directory: $dirname"
echo "Filename: $basename"
echo "Extension: $extension"

Simplifying User Input

#!/bin/bash
read -p "Enter your name: " name
name=${name:-"Anonymous"}
echo "Hello, $name!"

4. Conclusion

By mastering Linux parameter expansion, you can write more efficient Bash scripts, handle complex string operations, and automate routine tasks such as bulk renaming, path parsing, and user‑input handling. Continuous practice will turn you into a command‑line power user.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Command-linebashshell scriptingstring-manipulationparameter expansion
Ops Development & AI Practice
Written by

Ops Development & AI Practice

DevSecOps engineer sharing experiences and insights on AI, Web3, and Claude code development. Aims to help solve technical challenges, improve development efficiency, and grow through community interaction. Feel free to comment and discuss.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.