How do you create a file (i.e. Gist) in bash?
I want to create a gist in my bash shell, how can I do this in one line? In other words, I don't want to create an intermediate file.
I tried this but it failed to transfer the remote file:
source <(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)
Works on Mac OSX 10.9.
Thanks in advance.
source to share
Apple is shipping an older version of Bash, Bash 3.2; Bash 4 was released 5 years ago. Here are some possible ways to get around this:
- Install MacPorts , Homebrew or pkgsrc and install Bash through one of them; or just build and install it yourself from source . Remember to add the newly installed Bash to
/etc/shells
so you can set it as your shell, then go to System Preferences> Users & Groups, click the lock to specify your password so you can make changes, then right click (click two clicks / clicks) on your user to select "More options ..." and change your skin there. -
If you need to be compatible with the 7 year old Bash that comes with OS X, you can simply save the file and fix it from there. Here's a Bash example to make it easier:
function curlsource() { f=$(mktemp -t curlsource) curl -o "$f" -s -L "$1" source "$f" rm -f "$f" } curlsource https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
-
If you absolutely must avoid creating a temporary file and must work in ancient versions of Bash, your best bet is to read into a string and evaluate the result. I tried to mimic the effect
source <(cmd)
by creating a FIFO (named pipe), injecting output into itcmd
and reading it withsource
, but got nothing. It turns out, looking at the source for Bash 3.2, itsource
just reads the entire file into a line and checks the file size before that. The FIFO returns size 0 when you dostat
, sosource
happily allocates a string of length 1 (for a trailing zero), reads 0 bytes into it, and returns success. So, since itsource
just reads the entire file into a string and then evaluates that, you can simply do the same:eval "$(curl -s -L https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash)"
source to share
OS X still ships bash
3.2 by default; in this version, the command source
does not seem to work as expected with process substitutions, which can be demonstrated with a simple test:
$ source <(echo FOO=5)
$ echo $FOO
$
However, the same command source
works in bash
4.1 or newer (I don't have a setup for testing 4.0, and the release notes seem to disagree with this question.)
source to share