Skip to content
Advertisement

Non-dash related bad substitution error

I am trying to write a bash script performing to find and pull somewhere else a specific file type. So far, I came up with the following script:

find ./to_compress -type f -iname "*.tar" -mindepth 1 -maxdepth 1 -exec mv {} ./compressed/${{}##*/}

However the bash complains that ${{}##*/} is a bad substitution

bash: ./compressed/${{}##*/}: bad substitution

Some googling around suggested that it could due to -exec calling dash instead of bash, which I checked for with

find ./to_compress -type f -iname "*.tar" -mindepth 1 -maxdepth 1 -exec echo $0 {} ;

which responded with bin/bash xxxx, suggesting that bash was indeed called by the exec command.

What could this error be due to? How could I mediate it to get to the filename alone and remove the trailing directories?

Advertisement

Answer

The {} placeholder of the -exec flag of the find command is not a variable. You can only perform parameter expansion on variables. The problem has nothing to with the shell being dash or bash.

In this situation what you can do is run a sh with -exec, and pass the {} as a parameter to the sub-shell, like this:

find ... -exec bash -c 'mv "$1" "./compressed/${1##*/}"' -- {} ;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement