I have the following function split in my .bash_profile file.

function split {
   echo filename=$name extension=$ext

Now I should expect that the command split foo.bar will give me

filename=foo  extension=bar

But I get get -bash: ${$1%.*}: bad substitution error message. The same however works for usual shell variable in a shell script, say $x instead of $1 in .bash_profile (I think the same goes in .bashrc as well).

What's wrong and any remedy?


Drop the $ preceding the variable name (1) inside the parameter expansion:


you are already referring to the variable with the $ preceding the starting brace {, no need for another one in front of the variable name.

  • Also, you don't need quotes in the right-hand side of assignments. – Satō Katsura Sep 28 '16 at 20:26
  • @SatoKatsura That's true. – heemayl Sep 28 '16 at 20:27
  • @SatoKatsura Yeah realized that too. Thanks. – hbaromega Sep 28 '16 at 21:41

If you have the dirname, basename and awk commands, you might want to consider this function definition

function split {

DIR=$(/bin/dirname "$1")
BASE=$(/bin/basename "$1")
EXT=$(echo "$BASE" | /usr/bin/awk -F. 'NF>1 {print $NF}')
echo directory=$DIR filename=$NAME extension=$EXT


split good.bye/data.txt
split good.bye/data
split data.txt
split good.bye/data.001.txt
split "good bye/data 001.txt"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.