Explain Codes LogoExplain Codes Logo

Open an .html file with default browser using Bash on Mac

html
bash
macos
web-development
Alex KataevbyAlex Kataev·Jul 22, 2024
TLDR

You can launch an .html file in your default Mac browser using the open command followed by the file's name:

open yourfile.html

If the file is located elsewhere, you can specify the full path:

open /full/path/to/yourfile.html

This is equivalent to a double-click on the file in Finder.

Dive into open on Mac

On macOS, the Swiss-army-knife equivalent for opening any type of file is the open command. It liaises with LaunchServices, the macOS framework responsible for associating different file types with respective applications.

Understanding default apps

Here's how to summon the built-in macOS oracle (a.k.a man page 🧙‍♂️) for open command:

man open

This will give you a detailed rundown of different options available with the open command. For example, you may specify a specific application using -a option or use -e to open using TextEdit.

Explicit browser choice

If you wish to repaint the Mona Lisa in a different gallery (i.e., open in a non-default browser), use the -a switch with open:

open -a 'Google Chrome' yourfile.html # or open -a 'Firefox' yourfile.html

To deal with file paths that include spaces (party crashers, eh?), place the path within quotes or use a backslash (\) to escape spaces:

open -a 'Google Chrome' "/path/to/your file.html" # or open -a 'Google Chrome' /path/to/your\ file.html

Automating with scripts

You can include the open command within bash scripts or AppleScript to auto-magically launch files from the terminal.

For example, pairing the HTTP protocol with your beloved browser:

-- AppleScript snippet do shell script "open -a 'Google Chrome' http://www.example.com"

Setting custom file associations

The DefaultApplication utility from HAMsoft is a godsend when you need to define how specific files are handled. Make sure /usr/local/bin is on your shell's path:

# After installing DefaultApplication defaultapp set .html Google\ Chrome.app

Ensure your path settings are in order:

cat /etc/paths

to affirm the terminal's ability to locate necessary commands.

URLs with the open command

Our friend open isn't just a local-file-opening wizard—it handles URLs too, directing them to your default browser:

open http://www.example.com

This shows the Jack-of-all-trades side of open.

Open sesame beyond .html

The open command doesn't limit itself to just .html files—it can launch almost anything! Now that's a Swiss Army knife for you!

Troubleshooting den

If your file opens in TextEdit or another non-browser application, don't panic! Revisit your default application setting, correct it using Get Info via Finder, or employ the DefaultApplication utility.

Tips and Tricks

Consider creating aliases for your frequently-used browsers in your .bash_profile or .zshrc:

alias chrome="open -a 'Google Chrome'" alias firefox="open -a 'Firefox'" alias safari="open -a 'Safari'"

You can then quickly open any .html file in your desired browser:

chrome yourfile.html // The chrome finish touch

Indeed, these commands can become a lifeline (or LifeLine 😉, for Apex Legends fans) for all web developers, forming an essential part of their everyday file editing and testing operations.