Skip to content

Instantly share code, notes, and snippets.

@bryder
Last active May 18, 2016 04:25
Show Gist options
  • Save bryder/6818b6fb4b08a847ed09f4b063a9c225 to your computer and use it in GitHub Desktop.
Save bryder/6818b6fb4b08a847ed09f4b063a9c225 to your computer and use it in GitHub Desktop.
Reference: http://tldp.org/LDP/LG/issue18/bash.html
Given:
foo=/tmp/my.dir/filename.tar.gz
We can use these expressions:
path = ${foo%/*}
To get: /tmp/my.dir (like dirname)
file = ${foo##*/}
To get: filename.tar.gz (like basename)
base = ${file%%.*}
To get: filename
ext = ${file#*.}
To get: tar.gz
Note that the last two depend on the assignment made in the second one
if you want to get
/one/two/three/four.blah
and turn it into
/one/two
you can
i=/one/two/three/four.blah
echo ${i%/*/*}
/one/two
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment