LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
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
 
LinkBack Search this Thread
Old 03-14-2010, 10:55 PM   #1
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Bash: functional difference between process substitution and here string with $( ) ?


Hello

Further to solved LQ thread Bash: how to populate a list of arbitrarily named files?, what is the functional difference between feeding a loop with process substitution and feeding it with a here string with embedded command substitution?

ABSG pages: process substitution, here string and command substitution.

This works
Code:
while IFS= read -r -d '' file
do
   files+=("$file")
done < <(find $dir -type f -print0)
but this does not work
Code:
while IFS= read -r -d '' file
do
   files+=("$file")
done <<< "$(find $dir -type f -print0)"
Best

Charles
 
Old 03-15-2010, 01:47 AM   #2
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,008

Rep: Reputation: 110Reputation: 110
Process substitution sets up a FIFO and expands to the device file that fifo is associated with (/dev/fd/42 or somesuch). As such, all data from the command is redirected, doesn't matter what's in it.

The command substitution and here-string breaks because bash first does the command substitution, which chomps newlines from the end and cuts everything past a null in the data, since nulls aren't allowed in a string. Then, the here-string actually creates a here-document which actually creates a temporary file containing the string plus one newline at the end (since we chomped all of them earlier, but a valid text file must end each line with a newline).

Short story, the only way command substitution and here-string is equivalent to process substitution is when the content has exactly one newline at the end and no nulls whatsoever.

Last edited by tuxdev; 03-15-2010 at 01:50 AM.
 
1 members found this post helpful.
Old 03-15-2010, 02:08 AM   #3
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,514
Blog Entries: 27

Original Poster
Rep: Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174Reputation: 1174
Thank you for that very clear explanation tuxdev
 
  


Reply

Tags
bash, command substitution


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
string substitution lipun4u Linux - Newbie 3 02-22-2010 02:19 AM
Help with a sed substitution string hawgwild Programming 2 09-24-2009 02:35 PM
String Substitution Swapna173 Linux - Newbie 1 08-04-2008 06:22 AM
Substitution of a long string horacioemilio Programming 5 04-07-2008 09:21 PM
Bash Process Substitution joshholt Programming 4 10-11-2005 03:15 AM


All times are GMT -5. The time now is 05:31 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration