Site Map - skip to main content

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes every weekday Monday through Friday.
This page was generated by The HPR Robot at

hpr1648 :: Bash parameter manipulation

A summary and aide memoire of Bash parameter expansion methods

<< First, < Previous, , Latest >>

Thumbnail of Dave Morriss
Hosted by Dave Morriss on 2014-11-26 is flagged as Explicit and is released under a CC-BY-SA license.
Bash, script, parameter, variable. 8.
The show is available on the Internet Archive at:

Listen in ogg, spx, or mp3 format. Play now:

Duration: 00:40:33

Bash Scripting.

This is an open series in which Hacker Public Radio Listeners can share their Bash scripting knowledge and experience with the community. General programming topics and Bash commands are explored along with some tutorials for the complete novice.

Bash parameter manipulation

I'm a great fan of using the Linux command line and enjoy writing shell scripts using the Bash shell.

  • BASH (or more usually Bash or bash) is the name of a Unix shell. The name stands for Bourne Again SHell, which is a play on words. Bash is an extension of the shell originally written by Stephen Bourne in 1978, usually known as SH.

  • Bash was written as part of the GNU Project which forms part of the Linux Operating System.

  • A shell is the part of the operating system that interprets commands, more commonly known as the command line.

  • A knowledge of Bash is very helpful if you would like to be able to use the power of the command line. It is also the way to learn how to build Bash scripts for automating the tasks you need to perform.

In this episode we look at what parameters are in Bash, and how they can be created and manipulated. There are many features in Bash that you can use to do this, but they are not easy to find.

As I was learning my way around Bash it took me a while to find these. Once I had found them I wanted to make a "cheat sheet" I could stick on the wall to remind me how to do things. I am sharing the result of this process with you.

The version of Bash which I used for this episode is 4.3.30(1)-release

The full notes for this episode are to be found here:


Subscribe to the comments RSS feed.

Comment #1 posted on 2014-11-27 18:23:23 by Tom Rodman

Thx for covering bash substring expansion

Enjoyed your podcast. Thanks for your work. I'll have to start using the substring feature. --

Another example:

Ex $ forwork=Mustang $ car=forwork $ echo ${!car} Mustang

Ex $ set -- joy pain bliss; myargc=$#; echo ${!myargc} bliss $ set -- joy pain bliss; myargc=$#; echo ${!$#} bash: ${!$#}: bad substitution

More bash tips at:

Comment #2 posted on 2014-11-29 18:22:36 by Mike Ray

Great stuff

Thanks for a great podcast Dave. Learned some stuff I didn't know.

I particulalry like:

echo ${var:?undefined}

And, something I've now incorporated into some scripts I have for conversion of one audio file type into another:


Comment #3 posted on 2014-11-29 22:41:45 by Dave Morriss

Thanks Tom

Glad you enjoyed the podcast and found it useful.

I didn't want this episode to go too deep into Bash, so I deliberately drew the line at dealing with indirect references and positional parameters. I was almost ready to cover indirection, but finally decided not to. Perhaps next time!

Your example of 'echo ${!$#}' failing is, I assume, because Bash performs just one scan for parameter substitutions. In this case, even if it performed two passes, this would resolve to 'echo ${!3}' which returns nothing.

I tried this:

$ set -v -- joy pain bliss; myargc=$#; eval echo \${!$#}

which does do two passes. First time the backslash is dropped and $# returns 3 and second time Bash executes 'echo ${!3}' which does nothing. It's not illegal this time, but is counter-intuitive.

This one returns 'bliss':

$ set -v -- joy pain bliss; myargc=$#; ind=myargc; eval echo \${!$ind}

Bash is pretty cool!

Comment #4 posted on 2014-11-29 22:54:04 by Dave Morriss

Thanks Mike

This one was fun to do particularly because it helped to drum this stuff into my head.

I find myself using the suffix removal trick quite often. For example, today I typed the following one-liner to make ImageMagick convert some JPEG files to PNG and reduce them to a more manageable size:

for f in P*.JPG; do convert $f -resize 640 ${f%.JPG}.png; done

Glad you found it useful.

Comment #5 posted on 2014-12-04 10:40:14 by Jon Kulp

Geez just when I think I'm pretty good at something, along comes Dave to show me a whole category of cool bash tricks that I never tried before. Thanks :)

Comment #6 posted on 2014-12-05 21:30:57 by Dave Morriss

Thanks Jon

Glad you liked it. Thanks for the feedback :-)

Comment #7 posted on 2014-12-10 19:32:11 by musicpeace

Thanks Dave! & also for Magnatune

This was a really interesting topic to hear over audio, and your notes are great. Great that you mentioned your past podcast about Magnatune; Looks like a great distribution model for artists (&music fans). I look fwd to working through these, as well as other music apis like Soundcloud, as well as hearing your recent show about podcast/audio. Peace ;

Comment #8 posted on 2014-12-13 22:52:16 by Dave Morriss


Yay musicpeace.

Glad you liked the show. Yes Magnatune is great. John Buckman, the founder, is an impressive guy. The music really suits my tastes and is good value. I have a lifetime membership.

Leave Comment

Note to Verbose Commenters
If you can't fit everything you want to say in the comment below then you really should record a response show instead.

Note to Spammers
All comments are moderated. All links are checked by humans. We strip out all html. Feel free to record a show about yourself, or your industry, or any other topic we may find interesting. We also check shows for spam :).

Provide feedback
Your Name/Handle:
Anti Spam Question: What does the letter P in HPR stand for?
Are you a spammer?
What is the HOST_ID for the host of this show?
What does HPR mean to you?