LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-08-2023, 03:41 AM   #1
nyarukoooo
LQ Newbie
 
Registered: Dec 2023
Posts: 2

Rep: Reputation: 0
Why xargs echo cannot work as the mid seat in a pipeline?


I am trying to echo the content of quotes.txt and also use the say command to speak it:

cat quotes.txt | xargs echo | say

The say command did work, but the content of quotes.txt didn't show in my terminal, which means echo didn't take effect but it indeed passed the content to the say command.

does anyone know why echo didn't work as the mid-seat in a pipeline?

Thank you!
 
Old 12-08-2023, 03:59 AM   #2
nyarukoooo
LQ Newbie
 
Registered: Dec 2023
Posts: 2

Original Poster
Rep: Reputation: 0
I think I know the reason. Sorry guys
 
Old 12-08-2023, 05:18 AM   #3
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,798

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
The pipe redirects the echo output to the following command.
If you want both the terminal and "say" then you must duplicate it.
One method is "tee" that writes to stdout AND to a file.
And if the file is a special file that forwards to the terminal then it prints immediately.
Code:
xargs echo < quotes.txt | tee /dev/tty | say
xargs does nothing but changing embedded newlines to spaces. Perhaps you can skip it?
Code:
tee /dev/tty < quotes.txt | say
Another special file is a "process substitution":
Code:
{ tee >( cat >&3 ) < quotes.txt | say; } 3>&1
Of course you can save an output to a file (redirection) or a variable (command substitution) and use the file or variable twice.
In this case you have a file already.
Code:
fn=quotes.txt; cat "$fn"; cat "$fn" | say

Last edited by MadeInGermany; 12-08-2023 at 05:22 AM.
 
Old 12-08-2023, 05:40 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,864

Rep: Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311Reputation: 7311
I guess
Code:
{ tee >&3 < quotes.txt | say; } 3>&1
can work too without cat
and also probably
Code:
tee >(say)
is enough
 
Old 12-08-2023, 06:29 AM   #5
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,798

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Ah yes, "cat" is just a place holder; not needed if "say" can be used there:
Code:
tee >( say ) < quotes.txt
Also saving the descriptor magic.
 
  


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
xargs: unmatched single quote; by default quotes are special to xargs unless you use Raakh5 Linux - Newbie 7 05-21-2014 07:26 PM
echo 0:$(echo 8*35*37*47 | bc) | xxd -r && echo $(id -un) Linuxanity LinuxQuestions.org Member Intro 1 08-15-2012 06:30 PM
grep | xargs -I echo $(foo; bar; echo $(fee; fi; fo; fum)) == questionable output.. GrapefruiTgirl Programming 11 12-07-2010 07:02 PM
ls | echo, I got blank, why can't echo take the 2nd seat in a pipeline? elinuxqs Linux - Newbie 6 11-24-2006 08:25 AM
Kphone echo (echo echo) scabies Linux - Software 0 10-18-2004 02:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:38 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