Previously I used source command like this:
source file_name
But what I’m trying to do is this:
echo something | source
Which doesn’t work.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Since source (or .) takes a file as argument, you could try process substitution:
source <(echo something)
Method 2
Your source command requires a file argument. You can get that in some shells with a process substitution, and this is because in the same way the shell replaces…
arg=$(echo hi)
…the echo bit there on the command-line with the subshell’s output, in the case of process substitution it replaces the subshell with a named file – usually /dev/fd/62 or something – some link to a file-descriptor. With a pipe the file descriptor is 0 so…
echo 'echo hi' | . /dev/fd/0
… /dev/stdin or whatever as the case may be should work just fine on any linux system – and many others besides. You can also use here-documents similarly:
. /dev/fd/3 3<<HI $(echo 'echo hi') HI
You can verify the way your shell handles process substitution, by the way:
(set -x; readlink <(:))
…which prints (in bash):
+ set +x + readlink /dev/fd/63 ++ : pipe:[2212581]
…and so we can see that the shell is doing the substitution and readlink is reading from an an anoymous pipe that it opens on file-descriptor 63.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0