Remove substring from front and back of variable
I don't think that's possible (but would love to be proved wrong). However, you can use an intermediate variable. The bash -c
run by -exec
is just a bash instance, like any other:
$ find . -type f
./foobarbaz
$ find . -type f -exec bash -c 'v=${0#./foo}; echo ${v%baz}' {} \;
bar
So I see no reason why this wouldn't work (if I understood what you're trying to do correctly):
find ./Source -name '*.src' \
-exec bash -c 'v=${0%.src}; \
myconvert -i "{}" -o "./Dest/${v#./Source/}.dst"' {} \;
The usual way is to do it in two steps:
x=foobarbaz
y=${x#foo} # barbaz
z=${y%baz} # bar
As this are "Parameter Expansions", a "Parameter" (variable) is needed to be able to perform any substitution. That means that y and z are needed. Even if they could be the same variable:
x=foobarbaz
x=${x#foo} # barbaz
x=${x%baz} # bar
find ./Source/ -name '*.src' -exec \
bash -c '
x=${1#./Source/}
myconvert -i "$1" -o "./Dest/${x%.src}.dst"
' shname {} \;
Where shname
is parameter $0
and the next {}
is parameter $1
to the bash call.
A simpler alternative is to do a cd ./Source
at the begining to avoid removing that part later. Something like:
cd ./Source ### Maybe test that it exist before cd to it.
shopt -s extglob nullglob ### Make sure that correct options are set.
for f in *.src **/*.src; do
echo \
myconvert -i "$f" -o "./Dest/${f%.src}.dst"
done
Once you are convinced that it does what you want, comment out the echo.