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

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












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?







share|improve this question




















  • Why do you expect sed -e "s/txt/TXT/g" to leave alone the second txt? is expanded before sed gets to see it.
    – Satō Katsura
    Oct 30 '17 at 10:58











  • Not sure I understand your question. I expect the "echo Specimen_before.txt Specimen_after.txt" gets printed by the echo, then it gets processed by the sed "|sed -e "s/txt/TXT/g"". I assume the expansion is done before the whole '-exec' command is invoked.
    – Venca B Spam
    Oct 30 '17 at 11:06










  • Ok, not I got it. Both Satō Katsura and Kusalananda answers lead me to understand that the "command substitution is executed before find even starts".
    – Venca B Spam
    Oct 30 '17 at 11:14










  • I actually think @SatōKatsura's comment is slightly wrong. is expanded by find, after sed has seen it ("it" being the string ).
    – Kusalananda
    Oct 30 '17 at 11:39










  • You are right, the sed is invoked before find due to the shell command substitution, which is done before the find is executed by the shell.
    – Venca B Spam
    Oct 30 '17 at 12:12















up vote
3
down vote

favorite












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?







share|improve this question




















  • Why do you expect sed -e "s/txt/TXT/g" to leave alone the second txt? is expanded before sed gets to see it.
    – Satō Katsura
    Oct 30 '17 at 10:58











  • Not sure I understand your question. I expect the "echo Specimen_before.txt Specimen_after.txt" gets printed by the echo, then it gets processed by the sed "|sed -e "s/txt/TXT/g"". I assume the expansion is done before the whole '-exec' command is invoked.
    – Venca B Spam
    Oct 30 '17 at 11:06










  • Ok, not I got it. Both Satō Katsura and Kusalananda answers lead me to understand that the "command substitution is executed before find even starts".
    – Venca B Spam
    Oct 30 '17 at 11:14










  • I actually think @SatōKatsura's comment is slightly wrong. is expanded by find, after sed has seen it ("it" being the string ).
    – Kusalananda
    Oct 30 '17 at 11:39










  • You are right, the sed is invoked before find due to the shell command substitution, which is done before the find is executed by the shell.
    – Venca B Spam
    Oct 30 '17 at 12:12













up vote
3
down vote

favorite









up vote
3
down vote

favorite











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?







share|improve this question












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?









share|improve this question











share|improve this question




share|improve this question










asked Oct 30 '17 at 10:52









Venca B Spam

182




182











  • Why do you expect sed -e "s/txt/TXT/g" to leave alone the second txt? is expanded before sed gets to see it.
    – Satō Katsura
    Oct 30 '17 at 10:58











  • Not sure I understand your question. I expect the "echo Specimen_before.txt Specimen_after.txt" gets printed by the echo, then it gets processed by the sed "|sed -e "s/txt/TXT/g"". I assume the expansion is done before the whole '-exec' command is invoked.
    – Venca B Spam
    Oct 30 '17 at 11:06










  • Ok, not I got it. Both Satō Katsura and Kusalananda answers lead me to understand that the "command substitution is executed before find even starts".
    – Venca B Spam
    Oct 30 '17 at 11:14










  • I actually think @SatōKatsura's comment is slightly wrong. is expanded by find, after sed has seen it ("it" being the string ).
    – Kusalananda
    Oct 30 '17 at 11:39










  • You are right, the sed is invoked before find due to the shell command substitution, which is done before the find is executed by the shell.
    – Venca B Spam
    Oct 30 '17 at 12:12

















  • Why do you expect sed -e "s/txt/TXT/g" to leave alone the second txt? is expanded before sed gets to see it.
    – Satō Katsura
    Oct 30 '17 at 10:58











  • Not sure I understand your question. I expect the "echo Specimen_before.txt Specimen_after.txt" gets printed by the echo, then it gets processed by the sed "|sed -e "s/txt/TXT/g"". I assume the expansion is done before the whole '-exec' command is invoked.
    – Venca B Spam
    Oct 30 '17 at 11:06










  • Ok, not I got it. Both Satō Katsura and Kusalananda answers lead me to understand that the "command substitution is executed before find even starts".
    – Venca B Spam
    Oct 30 '17 at 11:14










  • I actually think @SatōKatsura's comment is slightly wrong. is expanded by find, after sed has seen it ("it" being the string ).
    – Kusalananda
    Oct 30 '17 at 11:39










  • You are right, the sed is invoked before find due to the shell command substitution, which is done before the find is executed by the shell.
    – Venca B Spam
    Oct 30 '17 at 12:12
















Why do you expect sed -e "s/txt/TXT/g" to leave alone the second txt? is expanded before sed gets to see it.
– Satō Katsura
Oct 30 '17 at 10:58





Why do you expect sed -e "s/txt/TXT/g" to leave alone the second txt? is expanded before sed gets to see it.
– Satō Katsura
Oct 30 '17 at 10:58













Not sure I understand your question. I expect the "echo Specimen_before.txt Specimen_after.txt" gets printed by the echo, then it gets processed by the sed "|sed -e "s/txt/TXT/g"". I assume the expansion is done before the whole '-exec' command is invoked.
– Venca B Spam
Oct 30 '17 at 11:06




Not sure I understand your question. I expect the "echo Specimen_before.txt Specimen_after.txt" gets printed by the echo, then it gets processed by the sed "|sed -e "s/txt/TXT/g"". I assume the expansion is done before the whole '-exec' command is invoked.
– Venca B Spam
Oct 30 '17 at 11:06












Ok, not I got it. Both Satō Katsura and Kusalananda answers lead me to understand that the "command substitution is executed before find even starts".
– Venca B Spam
Oct 30 '17 at 11:14




Ok, not I got it. Both Satō Katsura and Kusalananda answers lead me to understand that the "command substitution is executed before find even starts".
– Venca B Spam
Oct 30 '17 at 11:14












I actually think @SatōKatsura's comment is slightly wrong. is expanded by find, after sed has seen it ("it" being the string ).
– Kusalananda
Oct 30 '17 at 11:39




I actually think @SatōKatsura's comment is slightly wrong. is expanded by find, after sed has seen it ("it" being the string ).
– Kusalananda
Oct 30 '17 at 11:39












You are right, the sed is invoked before find due to the shell command substitution, which is done before the find is executed by the shell.
– Venca B Spam
Oct 30 '17 at 12:12





You are right, the sed is invoked before find due to the shell command substitution, which is done before the find is executed by the shell.
– Venca B Spam
Oct 30 '17 at 12:12











1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










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 %sn" "before.txt" "$1" "after.txt" | sed "s/txt/TXT/g"' sh ';'





share|improve this answer




















  • I got it. Your "The command substitution is executed before find even starts." is the answer.
    – Venca B Spam
    Oct 30 '17 at 11:12










Your Answer







StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401384%2fpipelined-sed-does-not-work-on-found-filename-inside-bash-command-substitution-w%23new-answer', 'question_page');

);

Post as a guest






























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










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 %sn" "before.txt" "$1" "after.txt" | sed "s/txt/TXT/g"' sh ';'





share|improve this answer




















  • I got it. Your "The command substitution is executed before find even starts." is the answer.
    – Venca B Spam
    Oct 30 '17 at 11:12














up vote
1
down vote



accepted










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 %sn" "before.txt" "$1" "after.txt" | sed "s/txt/TXT/g"' sh ';'





share|improve this answer




















  • I got it. Your "The command substitution is executed before find even starts." is the answer.
    – Venca B Spam
    Oct 30 '17 at 11:12












up vote
1
down vote



accepted







up vote
1
down vote



accepted






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 %sn" "before.txt" "$1" "after.txt" | sed "s/txt/TXT/g"' sh ';'





share|improve this answer












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 %sn" "before.txt" "$1" "after.txt" | sed "s/txt/TXT/g"' sh ';'






share|improve this answer












share|improve this answer



share|improve this answer










answered Oct 30 '17 at 11:07









Kusalananda

105k14209326




105k14209326











  • I got it. Your "The command substitution is executed before find even starts." is the answer.
    – Venca B Spam
    Oct 30 '17 at 11:12
















  • I got it. Your "The command substitution is executed before find even starts." is the answer.
    – Venca B Spam
    Oct 30 '17 at 11:12















I got it. Your "The command substitution is executed before find even starts." is the answer.
– Venca B Spam
Oct 30 '17 at 11:12




I got it. Your "The command substitution is executed before find even starts." is the answer.
– Venca B Spam
Oct 30 '17 at 11:12

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f401384%2fpipelined-sed-does-not-work-on-found-filename-inside-bash-command-substitution-w%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Peggy Mitchell

The Forum (Inglewood, California)

Palaiologos