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

hpr2719 :: Bash Tips - 17

Arrays in Bash (part 2)

<< First, < Previous, , Latest >>

Thumbnail of Dave Morriss
Hosted by Dave Morriss on 2019-01-03 is flagged as Explicit and is released under a CC-BY-SA license.
Bash, array, indexed array, associative array. 2.
The show is available on the Internet Archive at:

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

Duration: 00:34:16

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 Tips - 17 (Additional auxiliary Bash tips)

Arrays in Bash

This is the second of a small group of shows on the subject of arrays in Bash. It is also the seventeenth show in the Bash Tips sub-series.

In the last show we saw the two types of arrays, and learned about the multiple ways of creating them and populating them. We also looked at how array elements and entire arrays are accessed.

Now we want to continue looking at array access and some of the various parameter expansion operations available.

Long notes

I have provided detailed notes as usual for this episode, and these can be viewed here.


Subscribe to the comments RSS feed.

Comment #1 posted on 2019-01-07 17:03:00 by Ken Fallon

Things I didn't know

Didn't know about Substring manipulation

Also found out why the following works

filepath=$(dirname -- "${source}") filename=$(basename -- "${source}") extension="${filename##*.}" filename="${filename%.*}"

Comment #2 posted on 2019-01-07 19:18:46 by Dave Morriss

Substring manipulation

This is a very cool feature which I use a lot!

I'd approach your example with a little script, 'pathparse' which shows you don't need 'dirname' or 'basename':

$ cat pathparse #!/usr/bin/env bash


directory="${path%/*}" filename="${path##*/}" prefix="${filename%.*}" suffix="${filename#*.}"

printf '%-9s: %s\n' \ 'Directory' "$directory" \ 'Filename' "$filename" \ 'Prefix' "$prefix" \ 'Suffix' "$suffix"

$ ./pathparse /etc/apt/sources.list Directory: /etc/apt Filename : sources.list Prefix : sources Suffix : list

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?