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

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

Great stuff

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

MP3=${M4A%.m4a}.mp3

Comment #3 posted on 2014-11-29T22:41:45Z 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.

Comment #4 posted on 2014-11-29T22:54:04Z 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-04T10:40:14Z 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-05T21:30:57Z by Dave Morriss

Thanks Jon

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

Comment #7 posted on 2014-12-10T19:32:11Z 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-13T22:52:16Z by Dave Morriss

Magnatune

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 :).