HOWTO · Bash

How to Open HTML File Using Bash

Learn how to open an existing HTML file from Bash on macOS or Linux. On macOS, use open for the default application or open -a to choose an installed browser; on Linux, xdg-open uses the default application.

On this page

To open an existing local HTML file from Bash, use the launcher for your operating system: macOS provides open, while many Linux desktop environments provide xdg-open. Each asks the desktop to open the file with its associated application, usually the default browser. These commands launch a local file; they do not start a web server or publish the page.

Open an HTML file with the default browser

First change to the directory containing the page, or use its path directly. On macOS, open ./index.html opens a file in the current directory. On Linux, use xdg-open ./index.html for the same default-application behavior. Replace index.html with the name of your existing file. For a file in another directory, supply an absolute path such as /home/alex/site/index.html, or a path relative to the current directory such as ../site/index.html.

The launcher hands the path to the desktop environment and returns control to the shell; it does not print the rendered page as terminal output. The browser displays the HTML. The exact application depends on the file association configured on that computer.

Open an HTML file on macOS

On macOS, the built-in open command uses the application associated with the file type. Run open ./index.html from the directory containing the file, or give the full path, for example open "$HOME/Sites/demo/index.html". Quoting a path protects spaces and other shell characters from being interpreted as separators. You can also type open and drag the file from Finder into the Terminal window to insert its path, then press Return.

To use a particular installed application, use open -a followed by its application name and the file path. For example, open -a "Google Chrome" ./index.html asks macOS to open the page in Google Chrome. You can use an installed browser’s displayed application name, such as Firefox, in the same form. This is macOS syntax; open -a is not the Linux command shown below.

Open an HTML file on Linux

In a Linux graphical desktop session, xdg-open ./index.html asks the desktop to open the page using the default application associated with HTML files. Supply an absolute or relative path as needed, quoting it when it contains spaces: xdg-open "$HOME/sites/my demo/index.html".

xdg-open is commonly supplied by desktop integration packages, but it is not guaranteed to be installed or useful in every Linux environment. A text-only server, container, or remote shell without a graphical session may have no browser to launch. Installing Homebrew on macOS does not make xdg-open the standard macOS launcher; use macOS’s open there.

Choose a browser and open multiple files

On macOS, open -a "Google Chrome" ./one.html ./two.html requests that both files open in Google Chrome. Replace the application name with an installed browser’s name when needed. The same open -a form works for one file or several paths.

On Linux, xdg-open is intended to open a URL or file through the default desktop application. It does not provide a portable option for selecting a browser by display name. If you need a particular browser, use that browser’s own command-line executable, if it is installed and available on your PATH; its name and options vary by distribution and browser. Browser-specific modes such as private browsing also use that browser’s own options, so they are not interchangeable with open or xdg-open options.

Check paths and command availability

A relative path is interpreted from the shell’s current working directory, not from the directory where the article or browser is located. Run pwd to see the current directory. Use ls to inspect its files, or change to the right directory with cd; then pass the HTML filename to the platform’s launcher. If the path contains spaces, quote the whole path. Make sure the file exists and that its spelling and capitalization match the path you provide.

To check the HTML file you plan to open, test that supplied path directly. Set target to the path you will pass to open or xdg-open; quote it so spaces stay part of the filename. This example reports a clear result and exits successfully whether the file is present or missing. In the supplied run, the default path was absent, so it printed HTML file not found: ./index.html.

target=${1:-./index.html}
if [[ -f "$target" ]]; then
  printf 'Found HTML file: %s\n' "$target"
else
  printf 'HTML file not found: %s\n' "$target"
fi

For example, run the check with bash check-html-path.sh "$HOME/Sites/demo page/index.html" after saving it in check-html-path.sh. The file passed to the script is the path being tested; no separate temporary file can make the check pass. After a Found HTML file result, use that same path with the platform’s launcher. This check verifies a regular file exists, but it does not validate the HTML contents or launch a browser.

This separate shell probe tests Bash’s file check on a temporary file; it does not check the HTML target:

file=$(mktemp)
trap 'rm -f "$file"' EXIT
[[ -f "$file" ]]

The probe completed successfully and produced no stdout.

If Bash reports that open or xdg-open cannot be found, check that you are using the command for your operating system. open is the macOS choice; on Linux, the desktop integration package that provides xdg-open may be missing. If the launcher runs but no browser appears, confirm that a graphical desktop session is available and that an application is associated with HTML files. For a chosen browser, confirm that it is installed and that the command or application name is correct.

A local preview is appropriate for checking a page on your own computer. If the page depends on web-server behavior, routes, or resources that require HTTP, launch a local development server separately and open its URL; open and xdg-open only hand a file or URL to a desktop application.