LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-27-2021, 09:26 AM   #16
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled

Quote:
Originally Posted by igadoter View Post
Don't get it
Code:
$(< /tmp/file)
is command output substitution - but where's command ? < /tmp/file is just redirection.
From the GNU Bash manual:
Quote:
The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).

Last edited by shruggy; 04-27-2021 at 09:28 AM.
 
Old 04-27-2021, 09:35 AM   #17
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
process substitution is when the < or > is put before (. Also in that case there is no $.
$( ) is a different thing, that is called subshell.
So
Code:
<( echo wobble )  # this is process substitution
$(< /etc/file)    # this is a simple redirection in a subshell
Do not mix them.
 
Old 04-27-2021, 09:42 AM   #18
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by shruggy View Post
Here are a couple of examples of how I used the output variant when doing Kakoune golf:
You got my attention.
 
Old 04-27-2021, 09:51 AM   #19
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Well, the problem was that the -f option of kak required a file argument. I could use /dev/stdout, but in the end choose >(cat) instead.

Last edited by shruggy; 04-27-2021 at 09:53 AM.
 
Old 04-27-2021, 10:01 AM   #20
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Hello world interactive programm
Code:
% echo hello $(< <(read World; echo $World) )
Mars
hello Mars
 
Old 04-27-2021, 10:38 AM   #21
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
Quote:
Originally Posted by pan64 View Post
$( ) is a different thing, that is called subshell.
Have to take issue with that. Though most of these substitutions do run in sub-shells.

$(< /tmp/file) is a special case of command-substitution that is a more efficient replacement for $(< /tmp/file cat ).

(< /tmp/file cat ) would be an example of a subshell with internal redirection.
 
Old 04-27-2021, 12:09 PM   #22
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
There are 3 cases:
Code:
$(< file)
in this case the subshell will do a redirection on its stdin and after that will exit, there is nothing [else] to do. The "result" is that the content of stdin will arrive to stdout without any modification (finally you will get the content of the file).
Code:
$(cat file)
in this case the subshell will fork/exec an additional command, a cat which will read the file and send the content to the stdout of the subshell. The result will be the same, just in this case an additional binary (cat) was involved too.
At the end they produce exactly the same result, just the former one is much more efficient.
Code:
$(<file cat)
Now the file will be redirected to stdin by the subshell, but the shell will execute an additional cat command which will process that input (and will do nothing, just pass it to stdout). Again, functionally identical, but inefficient as the second example. (the only difference is: the subshell will create the file descriptor - open the file and pass it to cat or cat will open the file)

Last edited by pan64; 04-27-2021 at 12:14 PM.
 
Old 04-27-2021, 12:21 PM   #23
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Thanks. Construction with <file even works at command line
Code:
 
% </tmp/file cat
world
and
Code:
% cat /tmp/file
world
of course one can put whatever wants instead of cat say
Code:
% </tmp/file dd
 
Old 04-27-2021, 02:05 PM   #24
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,792

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
Code:
$(< /tmp/file)
is specific to bash and zsh:
in this (sub shell) context, if the command is missing then stdin is connected to stdout, behaving like cat
Other shells need a command there:
Code:
$(cat < /tmp/file)
zsh does it even in a the main context:
Code:
< /tmp/file
reads the file, where other shells need
Code:
cat < /tmp/file
Consequently zsh writes the file (from stdin) with
Code:
> /tmp/file
where other shells need
Code:
cat > /tmp/file
Just seeing your last post - do you have zsh?

Last edited by MadeInGermany; 04-27-2021 at 02:09 PM.
 
Old 04-27-2021, 02:58 PM   #25
mimorek
Member
 
Registered: Feb 2013
Distribution: Debian (jessie)
Posts: 42

Rep: Reputation: Disabled
From the bash man page:
Quote:
The command substitution $(cat file) can be replaced by the equivalent but faster $(< file).
 
Old 04-27-2021, 08:13 PM   #26
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
Quote:
Originally Posted by MadeInGermany View Post
Just seeing your last post - do you have zsh?
Yes. But never used.
 
  


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
[bash] why is Process Substitution <() so much faster ?! hashbang#! Programming 4 11-26-2010 04:19 AM
[SOLVED] Bash: functional difference between process substitution and here string with $( ) ? catkin Programming 2 03-15-2010 02:08 AM
Problems with Process Substitution and scp - Solaris to Linux sdduuuude Linux - Newbie 3 09-28-2007 10:47 AM
Bash Process Substitution joshholt Programming 4 10-11-2005 03:15 AM
process substitution jk3us Linux - Software 1 12-02-2004 04:34 PM

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

All times are GMT -5. The time now is 03:04 PM.

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