LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash command substitution $(<datafile) (https://www.linuxquestions.org/questions/programming-9/bash-command-substitution-%24-datafile-4175736533/)

gerard4143 04-29-2024 05:03 AM

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.

pan64 04-29-2024 05:08 AM

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

gerard4143 04-29-2024 05:16 AM

Quote:

Originally Posted by pan64 (Post 6498825)
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?

pan64 04-29-2024 05:33 AM

There is no real command executed in the subshell, just stdin is forwarded to stdout.

boughtonp 04-29-2024 07:59 AM


 
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.


rknichols 04-29-2024 08:40 AM

Quote:

Originally Posted by boughtonp (Post 6498855)
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)."

NevemTeve 04-29-2024 09:12 AM

It could be:
Quote:

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


All times are GMT -5. The time now is 02:59 AM.