LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-29-2022, 11:14 AM   #1
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Rep: Reputation: 19
weird behaviour of redirection in command substitution in find -exec


Please explain the effect of redirection on this command:
Code:
~/tmp/find$ touch file file1
~/tmp/find$ find . -name f\* -exec bash -c "echo $(ls {})" \;     
ls: cannot access '{}': No such file or directory


~/tmp/find$ find . -name f\* -exec bash -c "echo $(ls {} 2>&1)" \;
ls: cannot access ./file1: No such file or directory
ls: cannot access ./file: No such file or directory
In the first case, $(ls {}) is substituted before find is called, which is normal.
But in the second case, for some reason, {} is substituted with file names and then ls can not find the already found files!
 
Old 07-29-2022, 05:33 PM   #2
doru
Member
 
Registered: Sep 2008
Distribution: Ubuntu 8.04 LTS Server
Posts: 138

Original Poster
Rep: Reputation: 19
In the first case, bash substitutes $(ls {}) on the command line with nothing, and the "ls: cannot access '{}': No such file or directory" message is printed through the stderr.

In the second case, bash substitutes $(ls {}) with "ls: cannot access '{}': No such file or directory", and then find substitutes '{}' with found files and echo prints the resulting message. ls is never invoked for a second time.
 
Old 07-30-2022, 06:38 AM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,863
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Code:
find . -name f\* -exec bash -c "echo $(ls {})" \;
first prints this:
Code:
ls: cannot access '{}': No such file or directory
then executes this:
Code:
find . -name f\* -exec bash -c "echo ''" \;
Code:
~/tmp/find$ find . -name f\* -exec bash -c "echo $(ls {} 2>&1)" \;
executes this:
Code:
~/tmp/find$ find . -name f\* -exec bash -c "echo ls: cannot access '{}': No such file or directory" \;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] weird find options variable substitution behaviour doru Programming 3 07-29-2022 10:04 PM
LXer: Exec - Process Replacement and Redirection in Bash LXer Syndicated Linux News 0 09-03-2019 12:03 PM
Command Substitution: $(command) vs `command` penguinbody Linux - Newbie 9 04-15-2016 07:00 AM
bash redirection with exec: how to specify fd with variable? ta0kira Programming 3 02-21-2010 07:09 PM
HP Photosmart weird weird weird.... Vlad_M Linux - General 5 02-20-2005 05:41 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:44 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration