Last Updated: February 25, 2016
·
534
· dustinmorado

csh variable modifiers in bash

csh and zsh have handy variable modifiers to handle paths and filenames. Here are the bash versions:

mypath=/foo/bar/baz.sh
myfile=baz.sh

*in csh: *

$ echo ${mypath:h}
/foo/bar
$ echo ${mypath:t}
baz.sh
$ echo ${myfile:r}
baz
$ echo ${myfile:e}
sh

*in bash: *

$ echo ${mypath%/*}
/foo/bar
$ echo ${mypath##*/}
baz.sh
$ echo ${myfile%.*}
baz
$ echo ${myfile##*.}
sh

Reference