---
title: "Why Parameter Expansion in Bash?"
description: "Most of us would have come across bash scripts that have the use of secondary commands for simple string manipulations, such."
url: "https://h3manth.com/scribe/why-parameter-expansion-bash/"
canonical_url: "https://h3manth.com/scribe/why-parameter-expansion-bash/"
markdown_url: "https://h3manth.com/scribe/blog/why-parameter-expansion-bash.md"
publishDate: "2012-12-01T00:00:00.000Z"
author: "Hemanth HM"
category: "python"
tags: ["python","bash"]
word_count: 181
reading_time_minutes: 1
x_markdown_tokens: 403
content_signal: "ai-train=yes, search=yes, ai-input=yes"
---

# Why Parameter Expansion in Bash?

> Most of us would have come across bash scripts that have the use of secondary commands for simple string manipulations, such.

Most of us would have come across bash scripts that have the use of secondary commands for simple string manipulations, such as `cut` and `awk` , but wonders can be done with parameter expansion in bash at very effective and efficient manner.

Even before diving into the why parameter expansion it's a must to know how it works!

**Checkout the below examples to see how parameter expansion works :**

~~${NAME} com.example.old.java ${NAME#_.} example.old.java ${NAME##_.} java ${NAME%%._} com ${NAME%._} com.example.old ~~

~~${FILE} /usr/local/labs/python.py ${FILE#_/} usr/local/labs/python.py ${FILE##_ /} python.py ${FILE%%/_} ${FILE%/_} /usr/local/labs/python ~~

**But, why parameter expansion?**

``

## Let's time using cut.

music=wow.mp3 TIMEFORMAT='real: %E' time for ((i=0; i<1000; i++)); do
    
    
          n=$(cut -d . -f 1 <<<"$x"); 
    

done > /dev/null

## real: 2.758

## Now P.E

music=wow.mp3 TIMEFORMAT='real: %E'; time for ((i=0; i<1000; i++)); do
    
    
          n=${x%%.*}; 
    

done > /dev/null

## real: 0.022

Mind blowing! 2.756 vs 0.022, the code has done the talking! :)

P.S : thanks to e36freak for TIMEFORMAT hack and greycat logic "%" is towards the right in a US keyboard layout and "#" is towdars the left ;)
