Bash's source command not working with a file

2019-05-07 19:15发布

问题:

I am trying to source a script file from the internet using curl, like this: source <( curl url ); echo done , and what I see is that 'done' is echoed before the curl even starts to download the file!

Here's the actual command and the output:

-bash-3.2# source <( curl --insecure https://raw.github.com/gurjeet/pg_dev_env/master/.bashrc ) ; echo done
done
-bash-3.2# % Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 2833 100 2833 0 0 6746 0 --:--:-- --:--:-- --:--:-- 0

I am not too worried about 'done' being echoed before or after anything, I am particularly concerned why the source command wouldn't read and act on the script!

This command works as expected on my LinuxMint's bash, but not on the CentOS server's bash!

回答1:

At first, I failed to notice that you're using Bash 3.2. That version won't source from a process substitution, but later versions such as Bash 4 do.

You can save the file and do a normal source of it:

source /tmp/del

(to use the file from your comment)

Or, you can use /dev/stdin and a here-string and a quoted command substitution:

source /dev/stdin <<< "$(curl --insecure https://raw.github.com/gurjeet/pg_dev_env/master/.bashrc)"; echo done


回答2:

Try this:

exec 69<> >(:);
curl url 1>&69;
source /dev/fd/69;
exec 69>&-;

This should force yer shell to wait for all data from the pipe. If that doesn't work this one will:

exec 69<> >(:);
{ curl url 1>&69 & } 2>/dev/null;
wait $!
source /dev/fd/69;
exec 69>&-;


回答3:

Does the following work?

file=$(mktemp)
curl --insecure -o $file https://raw.github.com/gurjeet/pg_dev_env/master/.bashrc 
source $file
rm $file


标签: bash shell url