Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pipe html to a browser
#!/bin/sh -e
#
# Usage: browser
# pipe html to a browser
# e.g.
# $ echo '<h1>hi mom!</h1>' | browser
# $ ron -5 man/rip.5.ron | browser
if [ -t 0 ]; then
if [ -n "$1" ]; then
open $1
else
cat <<usage
Usage: browser
pipe html to a browser
$ echo '<h1>hi mom!</h1>' | browser
$ ron -5 man/rip.5.ron | browser
usage
fi
else
f="/tmp/browser.$RANDOM.html"
cat /dev/stdin > $f
open $f
fi
@ken-reitz

This comment has been minimized.

Copy link

@ken-reitz ken-reitz commented Apr 25, 2010

Installation:

brew install browser

Or, if you must:

 sudo curl https://gist.github.com/raw/318247/browser -o /usr/local/bin/browser
 sudo chmod +x /usr/local/bin/browser
@jocap

This comment has been minimized.

Copy link

@jocap jocap commented Nov 10, 2010

Just in case anyone can't figure this out:
TextMate command:
cat $TM_FILE | /usr/local/bin/browser

@rleber

This comment has been minimized.

Copy link

@rleber rleber commented Dec 11, 2010

Small point, but "...hi mom!..." should be '...hi mom!...' in the usage example in the comments. Strong quoting is needed to avoid the dreaded "bash: !: event not found" error.

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Feb 8, 2011

/Users/humza/homebrew/bin/browser: line 2: syntax error near unexpected token '<'
'Users/humza/homebrew/bin/browser: line 2: '<title>301 Moved Permanently</title>' I get this for some reason

@codeinthehole

This comment has been minimized.

Copy link

@codeinthehole codeinthehole commented Mar 12, 2011

The URL for installation should be https:

sudo curl https://gist.github.com/raw/318247/browser -o /usr/local/bin/browser
sudo chmod +x /usr/local/bin/browser
@ghost

This comment has been minimized.

Copy link

@ghost ghost commented May 31, 2011

or with zsh, it is
open =(echo '<h1>zsh rulez!</h1>')

=(foo) takes the output of foo, writes it into a file in /tmp and returns the files path.

@Ps0ke

This comment has been minimized.

Copy link

@Ps0ke Ps0ke commented May 29, 2013

In bash you can do <(foo) to achieve the same as zsh's =(foo). But it doesn't work with the open(1) utility for me. How should it figure out the file type anyway?

@Zeokat

This comment has been minimized.

Copy link

@Zeokat Zeokat commented Mar 9, 2014

Zeokat usefull piece of code.

@geoff-nixon

This comment has been minimized.

Copy link

@geoff-nixon geoff-nixon commented Mar 22, 2014

Try

:; I="$(basename $0)"; [ -z $BROWSER ] && open=open || open=$BROWSER

[ $# -eq 0 ] && set -- -; [ "$1" = "-" ] && [ -t 0 ] &&
  echo "Usage: [ COMMAND | $I ] | [ $I < FILE ] | [ $I FILE ... ]" >&2 && exit

for what; do where="/tmp/$(od -N2 < /dev/urandom | openssl dgst -sha1).html"
  cat "$what" > "$where" && $open "$where"; sleep 1 && rm -f "$where" & done

Some advantages:

  • Respect $BROWSER. Can be BROWSER=lynx, BROWSER='open -a Firefox', etc.
  • No bashisms ($RANDOM) or zsh-isms (=(...)).
  • Correctly handles arguments that do not end in .html.
  • Correctly handles multiple arguments and shell globs.
  • Cleans up after itself.
  • Doesn't even need a shebang!

Or, you might prefer what I use: avoid leaving your terminal at all, and:

cat "$what" > "$where" && qlmanage -p "$where" >/dev/null 2>&1; sleep 1 ...
@Boldewyn

This comment has been minimized.

Copy link

@Boldewyn Boldewyn commented Apr 29, 2015

To show Markdown files in browsers, I found a way without temporary files: https://gist.github.com/Boldewyn/4311962

It uses data: URIs for fun and profit:

x-www-browser $(base64 -w0 | cat <(echo -n 'data:text/html;base64,') -)
#  ^--- Debian link to default browser
#               ^---- base64 slurps stdin as default
#                            ^--- concat the "data:" header, then the content
@csonuryilmaz

This comment has been minimized.

Copy link

@csonuryilmaz csonuryilmaz commented Dec 20, 2019

It's still working after ~10 years. 😊 Thanks!

Installed with brew install browser 👉 Tested on MacOS Mojave 10.14.6 👍

@benwoodward

This comment has been minimized.

Copy link

@benwoodward benwoodward commented Mar 2, 2020

Works perfectly! Thanks Chris. Oh.. and thanks for making Github! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.