LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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-29-2024, 05:03 AM   #1
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Rep: Reputation: 4
Bash command substitution $(<datafile)


From: Learning the Bash Shell 3rd Edition O'Reilly(Newham& Rosenblatt)

page 100, section Command Substitution:

"command substitution, which allows you to use the standard output of a command as if it were the value of a variable."

What is the standard output of the command in this example(where datafile is just some acsii text file)?

Code:
ans="$(<datafile)"
It looks like, to the uninitiated, the standard output of the command is the command substitution itself.
 
Old 04-29-2024, 05:08 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
in short it will put the content of that datafile into the variable ans.
< means redirection, the stdin (input) of the process will be coming from a file
$() means opening a subshell, in which the command above will be executed (which is just taking the content of that file and sending it to stdout)
" " means keeping the result of that shell (stdout) in one string, avoid splitting
 
Old 04-29-2024, 05:16 AM   #3
gerard4143
Member
 
Registered: Jan 2008
Location: P.E.I. Canada
Posts: 31

Original Poster
Rep: Reputation: 4
Quote:
Originally Posted by pan64 View Post
in short it will put the content of that datafile into the variable ans.
< means redirection, the stdin (input) of the process will be coming from a file
$() means opening a subshell, in which the command above will be executed (which is just taking the content of that file and sending it to stdout)
" " means keeping the result of that shell (stdout) in one string, avoid splitting
But its not really the standard output of the command or is it that the redirection operator must be used in a context where the input can be read and so the command substitution just obliges and reads it and then dumps the contents to its stdout?
 
Old 04-29-2024, 05:33 AM   #4
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,962

Rep: Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332Reputation: 7332
There is no real command executed in the subshell, just stdin is forwarded to stdout.
 
Old 04-29-2024, 07:59 AM   #5
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555Reputation: 2555

What that book apparently fails to state, (and the official Bash manual's Command Substitution also does not make clear), is that "$(<filename)" is a Bash extension, to optimize the commonly used "$(cat filename)".

One can consult the Bash source code, and see that the command substitution function has a specific check for whether the command starts with a stdin redirect, which shortcuts the bulk of the what the function would otherwise do, and simply reads the file.

i.e. Yes, this is a special case, which differs to how command substitution otherwise works.

 
1 members found this post helpful.
Old 04-29-2024, 08:40 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,783

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
Quote:
Originally Posted by boughtonp View Post
What that book apparently fails to state, (and the official Bash manual's Command Substitution also does not make clear), is that "$(<filename)" is a Bash extension, to optimize the commonly used "$(cat filename)".
The bash manual seems pretty clear to me:
"The command substitution $(cat file) can be replaced by the equivalent but faster $(< file)."
 
Old 04-29-2024, 09:12 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,872
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
It could be:
Quote:
"There is a non-Posix extension: the command substitution $(cat file) can be replaced by the equivalent but faster $(< file)."
 
  


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] Patterns as GNUplot datafile separators? Turbocapitalist Linux - Desktop 2 05-17-2020 06:42 AM
Command Substitution: $(command) vs `command` penguinbody Linux - Newbie 9 04-15-2016 07:00 AM
datafile recovery in Oracle on centos kapil17october Linux - Server 1 01-03-2013 07:29 AM
[SOLVED] Move MySql datafile to different location ethereal1m Slackware 2 08-28-2010 12:01 AM
How to corrupt the datafile in Oracle Ejdaha Linux - Newbie 2 03-01-2010 07:15 AM

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

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