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)" |
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 |
Quote:
|
There is no real command executed in the subshell, just stdin is forwarded to stdout.
|
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. |
Quote:
"The command substitution $(cat file) can be replaced by the equivalent but faster $(< file)." |
It could be:
Quote:
|
All times are GMT -5. The time now is 02:59 AM. |