
In the world of system administration and scripting, the ability to open a URL from the command line can be incredibly useful. This can be achieved using the Bash shell in Linux. In this article, we will explore how to open a URL in the Bash command line using various commands and techniques.
To open a URL in the Bash command line, you can use the xdg-open
command followed by the URL parameter. This command will open the URL in the default web browser. Alternatively, you can use the gnome-open
command or call a specific browser program directly to open the URL.
The xdg-open
Command
The primary command used to open a URL in Bash is xdg-open
. This command opens a file or URL in the user’s preferred application. If a URL is provided, it will open in the default web browser.
Here’s an example of how to use it:
xdg-open http://google.com
In this command, xdg-open
is the command, and http://google.com
is the URL parameter. The URL parameter tells the xdg-open
command which web address to open.
Opening Other Types of URIs
The xdg-open
command is not limited to URLs. It can also open other types of URIs (Uniform Resource Identifiers), such as images or files. For example:
xdg-open /tmp/foobar.png
This command will open the image file /tmp/foobar.png
with the default image viewer. In this case, /tmp/foobar.png
is the file path parameter, which tells the xdg-open
command which file to open.
Querying the Default Application
To find out what application is set as the default for a specific file type, you can use the xdg-mime
command. For example:
xdg-mime query default text/html
This command will display the default application for opening HTML files. xdg-mime
is the command, query
is the operation to perform, default
is the type of query, and text/html
is the MIME type parameter.
Setting the Default Application
If you want to set your preferred application for opening specific file types or URIs, you can go to System → Preferences → Preferred Applications and make the changes there.
Using gnome-open
xdg-open
and gnome-open
are similar commands, but gnome-open
does not know what to do with naked domains. However, you can create a function to make it easier to open naked domains with gnome-open
. Here’s an example:
function go { gnome-open http://$1 ; }
You can paste this function in your command line and then use the go
command followed by the domain name to open it in the default browser.
Calling a Specific Browser Directly
If you don’t want to use the default browser, you can call a specific browser program directly and provide the URL as an argument. For example:
chromium-browser https://youtube.com
or
firefox https://youtube.com
In these examples, chromium-browser
and firefox
are the commands, and https://youtube.com
is the URL parameter.
Conclusion
Opening a URL in the Bash command line can be achieved using various commands and techniques. The xdg-open
command is a versatile and convenient way to open URLs or other types of URIs in the default browser or preferred applications. However, you can also use gnome-open
or call a specific browser directly if you prefer. With these commands at your disposal, you can easily open any URL from the command line.
To open a URL in the Bash command line, you can use the xdg-open
command followed by the URL. For example, xdg-open http://example.com
will open the URL http://example.com
in the default web browser.
Yes, the xdg-open
command can open other types of URIs, such as files or images. Simply provide the file path or image path as the parameter to the xdg-open
command. For example, xdg-open /path/to/file.txt
will open the file file.txt
in the default text editor.
You can use the xdg-mime
command to query the default application for a specific file type. For example, xdg-mime query default text/html
will display the default application for opening HTML files.
Yes, you can set your preferred application for specific file types by going to System → Preferences → Preferred Applications and making the changes there.
Yes, an alternative to xdg-open
is the gnome-open
command. However, it may not handle naked domains. You can create a function to open naked domains with gnome-open
by using the go
command followed by the domain name. For example, go example.com
will open example.com
in the default browser.
Yes, you can open a URL in a specific browser by calling the browser program directly and providing the URL as an argument. For example, chromium-browser https://example.com
will open example.com
in the Chromium browser, and firefox https://example.com
will open it in Firefox.