Saturday, 14 February 2026

Parameter expansion

🔥 Understanding Bash Parameter Expansion (With Real Terminal Output)

In this post, we will explore powerful Bash parameter expansion features using real terminal examples, it helps to manipulate variables without calling external commands like sed, cut, awk etc.


1️⃣ Basic Variable Expansion

[root@oel01db Shell-Scripting]# name='Mahesh Karthya' [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo $name Mahesh Karthya [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo ${name} Mahesh Karthya [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo "${name}" Mahesh Karthya [root@oel01db Shell-Scripting]#

All three produce the same output.


❓ What is the advantage of putting {} and double quotes?

Without Quotes

echo $name

Becomes internally:

echo Mahesh Karthya

Bash sees two separate words because of the space.


With Quotes

echo "$name"

Becomes internally:

echo "Mahesh Karthya"

Now Bash sees one single string.

With echo we don't notice much difference.
So let’s try something practical.


2️⃣ Creating Directory — Real Difference

Without Quotes

[root@oel01db Shell-Scripting]# mkdir ${name} [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# ls -lrt | tail -2 drwxr-xr-x 2 root root 6 Dec 24 00:53 Mahesh drwxr-xr-x 2 root root 6 Dec 24 00:53 Karthya [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# date Wed Dec 24 00:53:27 IST 2025 [root@oel01db Shell-Scripting]#

Two directories were created because Bash split the variable at space.


With Quotes

[root@oel01db Shell-Scripting]# mkdir "${name}" [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# ls -lrt | tail -1 drwxr-xr-x 2 root root 6 Dec 24 00:54 Mahesh Karthya [root@oel01db Shell-Scripting]#

Now only one directory is created.

Always quote variables if they may contain spaces.


3️⃣ Get Length of a String

To get the length, use # inside parameter expansion:

[root@oel01db Shell-Scripting]# echo ${#name} 14 [root@oel01db Shell-Scripting]#

# returns the length of the variable.


4️⃣ Capitalization Operations

Reset value

[root@oel01db Shell-Scripting]# name='mahesh karthya' [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo $name mahesh karthya [root@oel01db Shell-Scripting]#

🔹 Capitalize First Letter

[root@oel01db Shell-Scripting]# echo "${name^}" Mahesh karthya [root@oel01db Shell-Scripting]#

🔹 Capitalize All Letters

[root@oel01db Shell-Scripting]# echo "${name^^}" MAHESH KARTHYA [root@oel01db Shell-Scripting]#

🔹 Capitalize Specific Letter

[root@oel01db Shell-Scripting]# echo "${name^^h}" maHesH kartHya [root@oel01db Shell-Scripting]#

🔹 Capitalize Specific Pattern

[root@oel01db Shell-Scripting]# echo "${name^^[mk]}" Mahesh Karthya [root@oel01db Shell-Scripting]#

5️⃣ Lowercase Operations

Reset value

[root@oel01db Shell-Scripting]# name='MAHESH KARTHYA' [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo $name MAHESH KARTHYA [root@oel01db Shell-Scripting]#

🔹 Lowercase All Letters

[root@oel01db Shell-Scripting]# echo "${name,,}" mahesh karthya [root@oel01db Shell-Scripting]#

🔹 Lowercase First Letter

[root@oel01db Shell-Scripting]# echo "${name,}" mAHESH KARTHYA [root@oel01db Shell-Scripting]#

🔹 Lowercase Specific Letter

[root@oel01db Shell-Scripting]# echo "${name,,K}" MAHESH kARTHYA [root@oel01db Shell-Scripting]#

6️⃣ Default Values for Positional Parameters

Script: pos-def.sh

[root@oel01db Shell-Scripting]# cat pos-def.sh name=${1:-Mahesh} echo "hello $name" [root@oel01db Shell-Scripting]#

With Argument

[root@oel01db Shell-Scripting]# bash pos-def.sh John hello John [root@oel01db Shell-Scripting]#

Without Argument

[root@oel01db Shell-Scripting]# bash pos-def.sh hello Mahesh [root@oel01db Shell-Scripting]#

name=$1 is simple assignment.

But:

name=${1:-Mahesh}

Means:

Take argument 1. If empty or not set, use "Mahesh".


Using Variable Expansion as Default

[root@oel01db Shell-Scripting]# echo $USER root [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# cat pos-def.sh name=${1:-$USER} echo "hello $name" [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# bash pos-def.sh hello root [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# bash pos-def.sh mahi hello mahi [root@oel01db Shell-Scripting]#

7️⃣ Mandatory Parameter Check

If your script must not run without an argument:

[root@oel01db Shell-Scripting]# cat pos-def.sh name=${1:?} echo "hello $name" [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# bash pos-def.sh pos-def.sh: line 1: 1: parameter null or not set [root@oel01db Shell-Scripting]#

Custom Error Message

[root@oel01db Shell-Scripting]# cat pos-def.sh name=${1:?"Usage: $0 <name> - Please provide a name as the first argument."} echo "hello $name" [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# bash pos-def.sh pos-def.sh: line 1: 1: Usage: pos-def.sh <name> - Please provide a name as the first argument. [root@oel01db Shell-Scripting]#

Even empty string fails:

[root@oel01db Shell-Scripting]# bash pos-def.sh '' pos-def.sh: line 1: 1: Usage: pos-def.sh <name> - Please provide a name as the first argument. [root@oel01db Shell-Scripting]#

8️⃣ String Replacement

[root@oel01db Shell-Scripting]# path='/home/oracle/foo.txt' [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo "$path" /home/oracle/foo.txt [root@oel01db Shell-Scripting]#

Using External Command

[root@oel01db Shell-Scripting]# echo "$path" | tr o O /hOme/Oracle/fOO.txt [root@oel01db Shell-Scripting]#

Using Parameter Expansion (First Occurrence)

[root@oel01db Shell-Scripting]# echo "${path/o/O}" /hOme/oracle/foo.txt [root@oel01db Shell-Scripting]#

Replace All Occurrences

[root@oel01db Shell-Scripting]# echo "${path//o/O}" /hOme/Oracle/fOO.txt [root@oel01db Shell-Scripting]#

Replace Pattern

[root@oel01db Shell-Scripting]# echo "${path//[ce]/O}" /homO/oraOlO/foo.txt [root@oel01db Shell-Scripting]#

9️⃣ dirname / basename Equivalent

[root@oel01db Shell-Scripting]# dirname "$path" /home/oracle [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# basename "$path" foo.txt [root@oel01db Shell-Scripting]#

Remove From Start

[root@oel01db Shell-Scripting]# echo "${path#*/}" home/oracle/foo.txt [root@oel01db Shell-Scripting]#

Remove Longest From Start

[root@oel01db Shell-Scripting]# echo "${path##*/}" foo.txt [root@oel01db Shell-Scripting]#

Remove From End (Suffix Removal)

[root@oel01db Shell-Scripting]# echo "${path%/*}" /home/oracle [root@oel01db Shell-Scripting]#

How Bash processes it:

Original: /home/oracle/foo.txt
Search from right → find /foo.txt
Remove it → Result /home/oracle

OperatorDirectionMatch SizeResult for /home/oracle/foo.txt
${path#*/}Left $\rightarrow$Shortesthome/oracle/foo.txt
${path##*/}Left $\rightarrow$Longestfoo.txt (Basename)
${path%/*}$\leftarrow$ RightShortest/home/oracle (Dirname)
${path%%/*}$\leftarrow$ RightLongest(empty)

🔟 Substring Using Parameter Expansion

[root@oel01db Shell-Scripting]# name='MAHESH KARTHYA' [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo "${name:0}" MAHESH KARTHYA [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo "${name:1}" AHESH KARTHYA [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo "${name:7}" KARTHYA [root@oel01db Shell-Scripting]#

With Length

[root@oel01db Shell-Scripting]# echo "${name:7:3}" KAR [root@oel01db Shell-Scripting]# [root@oel01db Shell-Scripting]# echo "${name:0:3}" MAH [root@oel01db Shell-Scripting]#

From Backward (Negative Index)

[root@oel01db Shell-Scripting]# echo "${name: -2}" YA [root@oel01db Shell-Scripting]#

⚠ Important: There must be a space before -2.

No comments:

Post a Comment

JFrog Artifactory - How to install

JFrog Artifactory OSS Installation Guide CentOS 9 + PostgreSQL 17 This guide provides a structured workflow to install JFrog Artifactory OSS...