Bash – Bad substitution inside xargs subshell

bashquotingshell

I'm using this command to process PNG files:

find . -iname "*png" -print0 | xargs -r0 --max-procs=4 -n1 sh -c 'pngnq -s1 $1 && advpng -z -4 -q ${$1%.*}-nq8.png' -

It seems to actually work fine, but also outputs this for each file that it processes:

-: 1: -: Bad substitution

I assume that I'm doing something wrong here ${$1%.*} – but I'm really not sure. The trailing dash is there on purpose, as per the docs here – and I can't just pipe the whole thing because advpng can't process things from stdin/out, sadly – so I have to use filenames.

Anyone know how to fix this so it doesn't have this error?

Best Answer

An obvious problem is the missing quoting:

... sh -c 'pngnq -s1 "$1" && advpng -z -4 -q "${1%.*}"-nq8.png' -

You are right about your assumption that ${$1%.*} is the real problem. You must not repeat the $ in the brackets.

The docs say nothing about a trailing dash, do they? Just about a name.

Related Question