Pipelined Sed does not work on found filename inside Bash command substitution when invoked from Find "-exec"

Venca B Spam

It looks like 'find', 'bash' and 'sed' in some cases does not work as one expects.

The following example should first create file 'sample.txt', then find the file and finally process it by '-exec' command. The executed command prints found filename, test specimens, and modified filename. The 'sed' command itself is used to replace 'txt' to 'TXT'.

touch sample.txt
find ./ -maxdepth 1 -name "*.txt" -exec echo {} $(echo Specimen_before.txt {} Specimen_after.txt |sed -e "s/txt/TXT/g") \;

The expected output is:

./sample.txt Specimen_before.TXT ./sample.TXT Specimen_after.TXT

Instead it produces:

./sample.txt Specimen_before.TXT ./sample.txt Specimen_after.TXT

(the example has been tested also with old-school command substitution through backquotes '`' with the same result)

What am I doing wrong?

Kusalananda

The command substitution is executed before find even starts. The actual command executed (after substitutions, expansions and quote removals etc.) is

find ./ -maxdepth 1 -name *.txt -exec echo {} Specimen_before.TXT {} Specimen_after.TXT ;

If you need to run anything fancy (pipes or multiple commands) with -exec, then start a separate shell to do it:

find . -maxdepth 1 -type f -name '*.txt' \
    -exec sh -c 'printf "%s " "$1"; printf "%s %s %s\n" "before.txt" "$1" "after.txt" | sed "s/txt/TXT/g"' sh {} ';'

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Bash reading users y/n answer does not work (read command inside while loop reading find output)

Passing filename as variable from find's exec into a second exec command

Command Substitution in find -exec

Why is `/bin/ping` not found when in a Bash command-substitution subshell?

Sourcing a bash script does not error when pipelined with &&

xargs sed and command substitution in bash

sed with -r and \1 substitution does not work together

Using a command inside a sed substitution

Use a bash function in a pipelined command

Launching an editor from a pipelined command

The find Command with -exec Does Not Run the -exec Command

Separate filename and path inside find command's -exec option

Using literal empty curly braces {} inside sed command from find -exec

Command NOT found when called from inside bash script

Sed command does not execute correctly when called from bash script and works fine when called from prompt

Why does bash replaces text from command substitution with text thereafter

"command not found" when running "lessc" from bash

How to escape backslashes in `sed` inside `find ... -exec`?

Command substitution does not work in Ubuntu shortcuts

sed command does not work with find -exec?

Sed command does not have the same effect when invoked by a Makefile

Find "command not found" when executed in bash loop

Linux -Bash find, - exec, and sed

Bash string substitution for sed argument: Sed: unknown command: `''

How does this command substitution with "cat" work?

Observer does not work from Command but inside Controller

In bash, reading from a socket through exec, via process substitution, doesn't work,

Running sed command with Python doesn't work as it does in bash?

What does the bash option -i actually do when bash is invoked from Python?