Site Map - skip to main content - dyslexic font - mobile - text - print

Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


hpr1648 :: Bash parameter manipulation

A summary and aide memoire of Bash parameter expansion methods

<< First, < Previous, Latest >>

Host Image
Hosted by Dave Morriss on 2014-11-26 is flagged as Explicit and is released under a CC-BY-SA license.
Listen in ogg, spx, or mp3 format. | Comments (8)

Part of the series: 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: http://hackerpublicradio.org/eps/hpr1648_full_notes.html


Comments

Subscribe to the comments RSS feed.

Comment #1 posted on 2014-11-27T18:23:23Z 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:

http://TRodman.com/blog

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.

This one returns 'bliss':

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

Bash is pretty cool!

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

Provide feedback
Your Name/Handle:
Title:
Comment:
Anti Spam Question: What does the P in HPR stand for ?
Are you a spammer →
Who hosted this show →
What does HPR mean to you ?