Open an .html file with default browser using Bash on Mac
You can launch an .html
file in your default Mac browser using the open
command followed by the file's name:
If the file is located elsewhere, you can specify the full path:
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:
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
:
To deal with file paths that include spaces (party crashers, eh?), place the path within quotes or use a backslash (\
) to escape spaces:
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:
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:
Ensure your path settings are in order:
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:
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
:
You can then quickly open any .html
file in your desired browser:
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.
Was this article helpful?