Difference between "Command substitution" and "Process substitution"


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Difference between "Command substitution" and "Process substitution"
# 1  
Old 12-15-2008
Difference between "Command substitution" and "Process substitution"

Hi,

What is the actual difference between these two? Why the following code works for process substitution and fails for command substitution?

Code:
while IFS= read -r line; do  echo $line; done < <(cat file)

executes successfully and display the contents of the file

But,
Code:
while IFS='\n' read -r line; do  echo $line; done < $(cat file)

gives error as: bash: $(cat file): ambiguous redirect (same results for backticks syntax instead of "$()")

Also, please clarify to me whether 'IFS' is really required in this while loop as it seems to be redundant because by default read command will fetches the entire line if only one argument is supplied and no delimiter is needed here to print every line???
Is there any use case(s) available that the IFS presence is mandatory to print each and every line of a file?

Last edited by royalibrahim; 12-15-2008 at 10:00 AM..
# 2  
Old 12-15-2008
I don't understand what you mean by command and process substitution. I see no point in using IFS in this example as you said yourself.

Also I would go by
Code:
while read -r line; do
     echo $line
done < file

Cat'ting the file is not needed.
# 3  
Old 12-15-2008
if you are using bash, try this
Code:
while IFS='\n' read -r line; do  echo $line; done <<< $(cat file)

<<< is here-string
# 4  
Old 12-15-2008
Hi,

$(...) means execute the command ... and print the output
to the command line.
<(...) and =(...) create a temporary file of the output and
redirect the output of this to the command line.

To understand the difference try:

Code:
diff $(ls) $(ls)

and

Code:
diff <(ls) <(ls)

The first will fail as diff expects input from a file.

HTH Chris
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. AIX

Apache 2.4 directory cannot display "Last modified" "Size" "Description"

Hi 2 all, i have had AIX 7.2 :/# /usr/IBMAHS/bin/apachectl -v Server version: Apache/2.4.12 (Unix) Server built: May 25 2015 04:58:27 :/#:/# /usr/IBMAHS/bin/apachectl -M Loaded Modules: core_module (static) so_module (static) http_module (static) mpm_worker_module (static) ... (3 Replies)
Discussion started by: penchev
3 Replies

2. Shell Programming and Scripting

Bash script - Print an ascii file using specific font "Latin Modern Mono 12" "regular" "9"

Hello. System : opensuse leap 42.3 I have a bash script that build a text file. I would like the last command doing : print_cmd -o page-left=43 -o page-right=22 -o page-top=28 -o page-bottom=43 -o font=LatinModernMono12:regular:9 some_file.txt where : print_cmd ::= some printing... (1 Reply)
Discussion started by: jcdole
1 Replies

3. UNIX for Dummies Questions & Answers

Using "mailx" command to read "to" and "cc" email addreses from input file

How to use "mailx" command to do e-mail reading the input file containing email address, where column 1 has name and column 2 containing “To” e-mail address and column 3 contains “cc” e-mail address to include with same email. Sample input file, email.txt Below is an sample code where... (2 Replies)
Discussion started by: asjaiswal
2 Replies

4. Shell Programming and Scripting

perl's substitution operator "s" with file contents?

Please show me how to make substitution over the contents of a file in a perl script. In a perl script, the core part of substitution operation is s/FINDPATTERN/REPLACEPATTERN/g; However, I cannot figure out how to make the substitution occur over the contents of a file. The following... (3 Replies)
Discussion started by: LessNux
3 Replies

5. Shell Programming and Scripting

awk command to replace ";" with "|" and ""|" at diferent places in line of file

Hi, I have line in input file as below: 3G_CENTRAL;INDONESIA_(M)_TELKOMSEL;SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL My expected output for line in the file must be : "1-Radon1-cMOC_deg"|"LDIndex"|"3G_CENTRAL|INDONESIA_(M)_TELKOMSEL"|LAST|"SPECIAL_WORLD_GRP_7_FA_2_TELKOMSEL" Can someone... (7 Replies)
Discussion started by: shis100
7 Replies

6. Solaris

difference between "root" and "usr" packages

Hi, could someone pls enlighten me on the difference between the "root" package and "usr" package? Like in this example: pkginfo -l SUNWGtku | grep -i desc DESC: GTK - The GIMP Toolkit (Usr) and pkginfo -l SUNWGtkr | grep -i desc DESC: GTK - The GIMP Toolkit (Root)... (6 Replies)
Discussion started by: masloff
6 Replies

7. Shell Programming and Scripting

bash ignores escaped $ and says "bad substitution"

Can someone tell me how to get the version of bash that I am running? I'm running cygwin bash on Windows XP at home and cygwin bash on Vista at work. Is this the version number for bash? $ uname -a CYGWIN_NT-6.0 US-SEA-L3BER9K 1.5.25(0.156/4/2) 2008-06-12 19:34 i686 Cygwin This is the... (2 Replies)
Discussion started by: siegfried
2 Replies

8. Red Hat

"service" , "process" and " daemon" ?

Friends , Anybody plz tell me what is the basic difference between "service" , "process" and " daemon" ? Waiting for kind reply .. .. (1 Reply)
Discussion started by: shipon_97
1 Replies

9. UNIX for Dummies Questions & Answers

Explain the line "mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'`"

Hi Friends, Can any of you explain me about the below line of code? mn_code=`env|grep "..mn"|awk -F"=" '{print $2}'` Im not able to understand, what exactly it is doing :confused: Any help would be useful for me. Lokesha (4 Replies)
Discussion started by: Lokesha
4 Replies
Login or Register to Ask a Question