Why doesn't bash have csh variable modifiers?

Why doesn't bash have csh variable modifiers?


Posix has specified a more powerful, albeit somewhat more cryptic,
mechanism cribbed from ksh, and bash implements it.

${parameter%word}
Remove smallest suffix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
smallest portion of the suffix matched by the pattern deleted.

x=file.c
echo ${x%.c}.o
-->file.o

${parameter%%word}

Remove largest suffix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
largest portion of the suffix matched by the pattern deleted.

x=posix/src/std
echo ${x%%/*}
-->posix

${parameter#word}
Remove smallest prefix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
smallest portion of the prefix matched by the pattern deleted.

x=$HOME/src/cmd
echo ${x#$HOME}
-->/src/cmd

${parameter##word}
Remove largest prefix pattern. The WORD is expanded to produce
a pattern. It then expands to the value of PARAMETER, with the
largest portion of the prefix matched by the pattern deleted.

x=/one/two/three
echo ${x##*/}
-->three


Given
a=/a/b/c/d
b=b.xxx

csh bash result
--- ---- ------
$a:h ${a%/*} /a/b/c
$a:t ${a##*/} d
$b:r ${b%.*} b
$b:e ${b##*.} xxx




Home FAQ