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.
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.
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.
echo 'echo hi' | . /dev/fd/0
) to make side-effects in the current shell (like setting aliases, environment etc.) because the commands in a pipe are run as child processes and their side-effect are lost when they are finished.
– pabouk - Ukraine stay strong
Mar 29 '22 at 07:20
<(command ...)
is not part of the POSIX specification. You can find it in ksh, bash and some other shells. See What is the portable (POSIX) way to achieve process substitution?. – pabouk - Ukraine stay strong Mar 29 '22 at 07:15source
isn't part of the POSIX specification either? – muru Mar 29 '22 at 10:38.
command is part of POSIX andsource
is just a more readable alias for it. See https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#dot – pabouk - Ukraine stay strong Mar 29 '22 at 12:01source
is explicitly in the question, so this question at least isn't about POSIX shells – muru Mar 29 '22 at 12:02sudo cat /root/.bashrc | bash
didn't work. But your command worked perfectly. – Shayan Mar 02 '23 at 10:04