Navigation Menu

Search code, repositories, users, issues, pull requests..., provide feedback.

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly.

To see all available qualifiers, see our documentation .

  • Notifications You must be signed in to change notification settings

Useful script to open and interact with Safari from terminal on MacOS. Simple tweak to use this script also with other web browsers e.g. Google Chrome, Mozilla Firefox, Opera.

antoniolanza1996/openSafariFromTerminal

Folders and files, repository files navigation, useful script to open and interact with safari from terminal on macos, 3 types of usage:.

  • safari.sh ----> simply open Safari
  • safari.sh --url URL_PARAM ----> open a tab in Safari with the site specified in URL_PARAM
  • safari.sh YOUR_QUERY_SEPARATED_BY_SPACE ----> open Safari and search "YOUR_QUERY_SEPARATED_BY_SPACE" on Google ( NOTE : if your query contains symbols which are Shell operators (e.g. ( , ) , & , ' , > ), you should put it in quotes as you can see in example usage 3.ii)

Examples of usage:

  • safari.sh ----> open Safari
  • safari.sh --url www.github.com ----> open https://www.github.com on Safari
  • safari.sh something to search on Google ----> find "something to search on Google" on Google
  • safari.sh "Where there's a will there's a way (proverb)" ----> find "Where there's a will there's a way (proverb)" on Google

Support for other web browsers

If you usually use another web browser on your Mac, you can easily adapt my script by changing BROWSER_PATH variable with the correct web browser installation path. For example:

Google Chrome

BROWSER_PATH="/Applications/Google Chrome.app"

Mozilla Firefox

BROWSER_PATH="/Applications/Firefox.app"

BROWSER_PATH="/Applications/Opera.app"

See https://github.com/antoniolanza1996/scripts_and_dotfiles/tree/main/bash_scripts for other useful bash scripts for MacOS.

  • Shell 100.0%

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Terminal command to open new tab in Safari

so heres my issue
while running command "open https://www.google.com"  in terminal consecutive times. I have noticed the second time i run the command it just shows the original window of google.com instead of opening in a new tab. It works fine when its a different website being requested
is this a mac os bug? ive tried setting new tabs to always in safari but no luck
Im trying to get safari to ALWAYS open a new tab even if its the same website

Mac mini, macOS 11.5

Posted on Sep 10, 2021 4:40 AM

Similar questions

  • Command + R does not refresh in safari anymore anyone have the same problem as Command + R does not refresh safari anymore? I'm using MacOS Monterey 12.0.1 1234 1
  • Command + L Not Working in Safari with Mac OS 11.6 update Command + L shortcut is not working in Safari with latest MacOS 11.6 update. Shortcut is working in Chrome browser. 1052 1
  • Safari won't go to website entered in address bar unless it is entered twice If I open a new tab in Safari (13.0.4) and then enter a website address in the address bar and hit return, the address bar just goes blank. If I then enter it a second time and hit return, it opens up the site. Is there a way to get around having to enter website addresses twice? It didn't do this until after I updated to Catalina. 526 2

Loading page content

Page content loaded

leroydouglas

Sep 10, 2021 7:18 AM in response to kenan201

kenan201 wrote:

Maybe you can paint a bigger picture what you are trying to accomplish here.

And what about Command T to open a new tab...(?)

Use tabs for webpages in Safari on Mac - Apple Support

Change Tabs preferences in Safari on Mac - Apple Support

Keyboard shortcuts and gestures in Safari on Mac - Apple ...

VikingOSX

Sep 10, 2021 7:19 AM in response to kenan201

The Terminal "open" command does not control how you interact with Safari, and cannot direct Safari to create a new tab. However, it can be done with a short Zsh (or Bash) script assigned to a shell as a function, alias, or in this example, a Zsh function that is available to you when you launch the Terminal application.

Put the following code in your newly created ~/.zshenv file. This file is automatically read by Zsh on opening a new Terminal, and the function will then be available for you to run on the Terminal command-line:

Once you have added this in the ~/.zshenv file, you can make it immediately active by:

And, to open a new browser tab in Safari, you run the function in the Terminal as:

Tested in macOS 11.5.2 on an M1 mini.

HWTech

Sep 11, 2021 5:24 PM in response to kenan201

You may be able to make it even simpler by using the "open" command like this:

I would suggest trying the "-n" option as well if it does not open a new window/tab. The "-n" option allows opening another instance of the app.

You will want to replace "<URL>" with the proper URL you want Safari to open. I suggest keeping the double quotes.

See this forum post:

https://superuser.com/questions/133748/running-safari-from-the-command-line-adds-current-directory-to-the-url

Sep 10, 2021 8:13 AM in response to leroydouglas

i have a program that for stock alerts and automatically opens default browser upon stock being found using terminal however it doesn't open new tabs correct

How to Launch OS X Apps from the Command Line

John Martellaro

[Note: A little bit of prior UNIX experience will be helpful here.]

First off, I will admit this technique is fairly geeky. In my [Mac] OS X career, I've only used this trick a handful of times. However, it's so darn cool and charming that one just aches to use it at any opportunity.

Actually, upon reflection, there are some useful scenarios for this technique.

  • The Finder acts up, a terminal window is already open, and you'd like to be able to launch a GUI app that might help you diagnose the problem.
  • You're a UNIX professional, live and breathe the command line, but you'd rather use a GUI text editor for coding instead of the raw and ugly vi or Emacs editors.
  • You're writing, say, a Perl or Python script, for another user and, at some point in the script, you'd like to launch a GUI app that carries the workflow forward.
  • You want to quickly run a utility with admin privileges without the restriction of the account you're logged into. See, for example, “ How to Find & Recover Missing Hard Drive Space. ” In that case, you must bypass the open command and drill into the Package Contents to run the app with sudo and admin privileges. But I've digressed.

The Man Page

The terminal app is in the Utilities folder—which is found the Applications folder. Launch it now. The BSD UNIX command we'll be using is open . Below is the manual page (“man” for short), shown by typing:

I've printed most of the man page for the open command here for your perusal, but I'm not going to explore every argument.  And feel free to jump right to the examples below to whet your appetite.

For this limited how-to, one interesting way to use the open command in this context is to use the [-a] option. Some other useful variations and arguments are in the examples below. 

Typical Examples

Here are some short and sweet examples of the open command with the -a, -e, and -t arguments. They're easy to try. [If you don't have BBEdit installed, use any other favorite a text editor in /Applications.]

These are just a few of the cool things you can do on the command line with the open command. To keep things simple, I haven't delved into creating a search path or other Unix tricks like aliases. All that's been left for you to explore on your own.

I think this is one of the neatest tricks in OS X.

__________________

Launch button via Shutterstock.

Leave a Reply Cancel reply

You must be logged in to post a comment.

This site uses Akismet to reduce spam. Learn how your comment data is processed .

  • John Martellaro

open safari with terminal

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

is there a way to open browser using terminal? [duplicate]

Is there a way to open Chrome (or other browser) using terminal?

Something like: $ chrome www.google.com ?

zombie_ghast's user avatar

  • 1 Do you want to open it to a specific web page or just open the browser? –  TheWanderer Commented Oct 7, 2015 at 11:57

3 Answers 3

From: Terminal command to open URL's in default and non-default browser (which I don't think is a duplicate)

If you want to open Chrome to a specific URL, just run

To open your default browser to a specific URL, run

If you need to run Chrome and close the terminal window afterward, run

>/dev/null 2>&1 will prevent messages from the browser to be outputted to the terminal's window; & will put the process into the background and disown will remove the job / process from the job list, preventing a SIGHUP signal to be propagated to it.

To do this with another browser simply replace google-chrome with that other browser's executable name.

Community's user avatar

  • Thanks! But there is a slight difference comparing to using a launcher. I will kill the process if I close terminal or ctrl+c. It's not a big deal though. –  zombie_ghast Commented Oct 7, 2015 at 12:15
  • @kos is it alright if I add this to my answer? –  TheWanderer Commented Oct 7, 2015 at 12:48
  • Sure, but use google-chrome http://www.google.com </dev/null >/dev/null 2>&1 & disown , because stdout and stderr get printed to the terminal without the redirections –  kos Commented Oct 7, 2015 at 12:59
  • @kos Is it correct? –  TheWanderer Commented Oct 7, 2015 at 13:13
  • Almost: >/dev/null 2>&1 prevents stdout and stderr to be printed to the terminal, </dev/null "closes" stdin , but to be honest that's not really necessary; & puts the process into the background and disown removes the process from the bash instance's jobs list, which prevents the SIGHUP signal sent to bash instance upon the terminal closure to be propagated to it –  kos Commented Oct 7, 2015 at 13:42

sensible-browser seems to be the option you're looking for. This will run the web browser set as default on your system, you can also pass parameters on it in order to run the web browser and open the given website(s).

In a terminal, drop the next and hit Return

Passing parameters:

The next command will open youtube.com in your preferred web browser:

How to set my favorite web browser flavour from the terminal?

Simply drop the next command in a terminal, hit Return and choose wisely:

In the next example I am choosing luakit as my default browser. You can change your default web browser as many times as you wish.

Unattaching from terminal

If you wish to keep your web browser running just after you close your terminal, simply add an ampersand symbol at the end of your command:

Geppettvs D'Constanzo's user avatar

  • 1 Nice, but without disown closing the terminal SIGHUPs it; you want to add disown and to redirect at least stdout and stderr to avoid messages to be still outputted to the terminal (and maybe stdin for "formal correctness"): sensible-browser [parameters] </dev/null >/dev/null 2>&1 & disown –  kos Commented Oct 7, 2015 at 13:05
  • Is "sensible-browser" supported all Linux flavors? Or is it just certain distributions like Debian, etc.? –  Ben Dev Commented Jun 6, 2021 at 7:39
  • AFAIK sensible-browser is available on every Ubuntu flavor. I haven't tried this on other Linux systems. –  Geppettvs D'Constanzo Commented Jun 7, 2021 at 19:32

You can open Google Chrome from terminal very surprisingly by running

You can also start Firefox by running

And you can add an URL as a parameter to open it too.

Mark Kirby's user avatar

  • Awesome! But there is a slight difference comparing to using a launcher. I will kill the process if I close terminal or ctrl+c. It's not a big deal though. –  zombie_ghast Commented Oct 7, 2015 at 12:14
  • once I refreshed page a bit of your answer was gone, but I think it's useful to keep it: nohup google-chrome & –  zombie_ghast Commented Oct 7, 2015 at 12:57
  • That does not work. It still closes Chrome. You may need to use screen . –  Pilot6 Commented Oct 7, 2015 at 13:10

Not the answer you're looking for? Browse other questions tagged bash browser .

  • The Overflow Blog
  • Mobile Observability: monitoring performance through cracked screens, old...
  • Featured on Meta
  • Announcing a change to the data-dump process
  • Bringing clarity to status tag usage on meta sites

Hot Network Questions

  • Are old games from 2023 considered in new fide rating calculations?
  • How to translate the German word "Mitmenschlich(keit)"
  • All four children have England birth index page changes
  • World Building Knowledgebase - How to write good Military World Building
  • I have two identical LaTeX files, with different file names. Only one yields "undefined references." Why is this happening?
  • How much easier/harder would it be to colonize space if humans found a method of giving ourselves bodies that could survive in almost anything?
  • Bash script that takes multiple path arguments and checks if files can be successfully created there
  • Can an APK be installed from a URI via `adb`?
  • A SF novel where one character makes a "light portrait" of another one, with huge consequences
  • Word to describe telling yourself that you are not, and will never be, good enough
  • Converting pipe-separated file containing GeoJSON geometries to GeoJSON with ogr2ogr
  • What happens to entropy during compression?
  • Explain how π – 1 + 50 + ⅔ × 1000 is PLONK
  • Self-descriptive
  • Seinfeldisms in O.R
  • Identifications in differential geometry
  • Can taut membranes and strings that are clamped at both ends propagate non-standing waves?
  • Why is there an "unnamed volcano" in Syria?
  • How can coordinates be meaningless in General Relativity?
  • A SF novel where a very poor girl finds a "talking book" meant for daughters of extremely rich people
  • Do eternal ordinances such as the festival of unleavened bread pose a biblical contradiction?
  • Conservation of the determinant of density matrix
  • Understanding a proof that a bounded sequence in a separable Hilbert space contains a weakly convergent subsequence
  • Is it a date format of YYMMDD, MMDDYY, and/or DDMMYY?

open safari with terminal

Stack Exchange Network

Stack Exchange network consists of 183 Q&A communities including Stack Overflow , the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Opening a URL from the terminal in different spaces macOS

If I run open https://example.com from the terminal and then switch to another space with a safari window open and run the same command, the safari window in my current space will just get focused and the URL itself will not open.

How can I force the open command to switch me to the other space if that is where the URL is open or just always open it in the current space?

  • shell-script

Anmol Sethi's user avatar

python -mwebbrowser http://example.com

Did some googling and that does what I want. It switches me to the other space since thats where the URL is open. I wish there was a way to just force opening in my current space always.

  • My real account is this whoops. –  nhooyr Commented Aug 13, 2018 at 6:47

You must log in to answer this question.

Not the answer you're looking for browse other questions tagged macos browser shell-script automation ..

  • The Overflow Blog
  • Mobile Observability: monitoring performance through cracked screens, old...
  • Featured on Meta
  • Announcing a change to the data-dump process
  • Bringing clarity to status tag usage on meta sites

Hot Network Questions

  • Could there be a runaway thermonuclear fusion in ocean of heavy water?
  • Whats the safest way to store a password in database?
  • Reheating beans makes them mushy and unpeeled
  • Doesn't counting hole and electron current lead to double-counting of actual current?
  • How to setup a home lab with a custom domain name?
  • Does measuring a quantum object collapse the wave function even if the particle is not found in the position where it was measured?
  • Self-descriptive
  • Driving relay without a control Fet: Back EMF consideration when control side is Grounded
  • Can an APK be installed from a URI via `adb`?
  • Seinfeldisms in O.R
  • Can Ontario municipal zoning by-laws prohibit site-built tiny homes?
  • In 1982 Admiral Grace Hopper said "I still haven't found out why helicopter rotors go the way they do". If she were here today, how might one answer?
  • Can it be acceptable to take over CTRL + F shortcut in web app
  • A SF novel where a very poor girl finds a "talking book" meant for daughters of extremely rich people
  • Is it a date format of YYMMDD, MMDDYY, and/or DDMMYY?
  • A SF novel where one character makes a "light portrait" of another one, with huge consequences
  • How can I align this figure with the page numbering?
  • Why is there an "unnamed volcano" in Syria?
  • set face for text in izones
  • ESTA is not letting me pay
  • Are there physical cases where we deliberately randomise the events?
  • Does a readied action have to be an action?
  • How to reproduce this equation itemization?
  • What did Wittgenstein mean by ”contradiction is the outer limit of propositions”?

open safari with terminal

We've detected unusual activity from your computer network

To continue, please click the box below to let us know you're not a robot.

Why did this happen?

Please make sure your browser supports JavaScript and cookies and that you are not blocking them from loading. For more information you can review our Terms of Service and Cookie Policy .

For inquiries related to this message please contact our support team and provide the reference ID below.

IMAGES

  1. Use this TERMINAL COMMAND to Open the Safari Application On a Mac

    open safari with terminal

  2. How to always open Safari in a private window

    open safari with terminal

  3. Untick the 'Open Safe Files' preference in Safari with Terminal

    open safari with terminal

  4. How To Open Safari in Full Screen?

    open safari with terminal

  5. How to Open Multiple Windows of Safari on iPad (iOS 16)

    open safari with terminal

  6. How to Open Multiple Windows of Safari on iPad (iOS 16)

    open safari with terminal

VIDEO

  1. How to change the home page in Safari

  2. How to Use Multiple Java (JDK) Versions on macOS in 2024

  3. Fix✅: Safari Can't Open The Page Because It Couldn't Establish A Secure Connection To The Server

  4. Safari for Windows default opening animation 4K

  5. Fixed: Safari Cannot Open The Page Because The Address is Invalid

  6. Safari cannot open the page because it could not establish a secure connection to the server iphone

COMMENTS

  1. How to open Google Chrome or Safari from Terminal

    All you need to do in Terminal, no mater what the PWD, is use open -a Safari or open -a Google\ Chrome and if wanted, add the URL afterwards if you want to open the Browser to the given URL. You do not need the type the pathname or the .app extension .

  2. macos

    to open a browser (safari as an e.g. here). The open -a stand for "opens with the specified application" . It is case-insensitive, so you can open Finder like

  3. Run Safari from command line with URL parameter

    According to the man page for open (1), open -a <applicationName file> opens a given file with a given application. Therefore, this seems like the most appropriate syntax. This forum thread explain more. The correct syntax would be open -a Safari --args example.com, however Safari doesn't seem to have any documented example for opening a URL ...

  4. macos

    Stack Exchange Network. Stack Exchange network consists of 183 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.. Visit Stack Exchange

  5. java

    You could use the open command on MacOS. (see man open for more information about this command).. In your case you can write something like that: open -a Safari https://www.stackoverflow.com and it works. (NB: Remember to specify the protocol!) In addition, I wrote an useful script to interact with Safari from MacOS and one of the options of this script is exactly what you want, see it on ...

  6. Open a new Safari window in the current Space from Terminal with

    function Safari { # Will open a New Safari window with argument 1. osascript <<EOD tell application "Safari" to make new document with properties {URL:"$1"} return EOD } In terminal: cd to your home directory. Run: nano .profile If you already have a .profile file it will open and there will be some code there already.

  7. Useful script to open and interact with Safari from terminal on MacOS

    Useful script to open and interact with Safari from terminal on MacOS. Simple tweak to use this script also with other web browsers e.g. Google Chrome, Mozilla Firefox, Opera. - antoniolanza1996/op...

  8. Open an URL in Safari with Private Browsing

    Safari does not have similar command line options like Google Chrome, however, here is a way to roll your own:. In Terminal, run the following compound command:. touch safari; open -e safari; chmod +x safari Copy and paste the example AppleScript code, shown further below, into the opened safari document, then save it.. You should then move the safari shell script to a directory located within ...

  9. How to Open Browser Tab From the Command Line on Mac

    If you want to find out the "official" names of the browsers on your Mac, open a new terminal window (or cd into the root of your machine) and type the following commands. root: ... Android Studio.app RingCentral.app Brave Browser.app Safari.app Docker.app Slack.app ...

  10. Terminal command to open new tab in Safari

    The Terminal "open" command does not control how you interact with Safari, and cannot direct Safari to create a new tab. However, it can be done with a short Zsh (or Bash) script assigned to a shell as a function, alias, or in this example, a Zsh function that is available to you when you launch the Terminal application.

  11. How to Launch OS X Apps from the Command Line

    1. open -a Safari [The default path is /Applications] 2. open -a BBEdit [Just launch the app, no file specified.] 3. open -a BBEdit test.txt [Lauch BBEdit and load the specified file from home dir ...

  12. is there a way to open browser using terminal?

    To open your default browser to a specific URL, run. If you need to run Chrome and close the terminal window afterward, run. >/dev/null 2>&1 will prevent messages from the browser to be outputted to the terminal's window; & will put the process into the background and disown will remove the job / process from the job list, preventing a SIGHUP ...

  13. Reset Safari from command line

    1 - Open terminal. 2 - Issue killall Safari. 3 - Go to the list of apps (Four fingers swipe-inwards) 4 - Shift + Click Safari. Share. Improve this answer. Follow answered Jun 17, 2019 at 0:37. nehem nehem. 250 1 1 gold badge 2 2 silver badges 12 12 bronze badges. Add a comment | 0

  14. Open multiple Safari tabs from the command line on Mac OS X

    Here's how to change Safari's general settings to open new URLs in new tabs: Make Safari open new links in existing windows as a tab, rather than a new window I don't know if there's a way to do it by specifying an option at the command line.

  15. How To Open Any Folder From The Mac Terminal

    open -a Safari https://www.google.com This opens the URL in Safari instead of the default browser. ... This will open a Terminal window with the current directory set to the folder you selected.

  16. How to Open the Browser Console on Chrome, Safari, Firefox ...

    Here's a list with some useful shortcuts that you can use inside the built-in console of Microsoft Edge: Launching the Console in focus mode. Ctrl + Shift + J. Switching to the Console. Ctrl + 2. Show or hide the Console from another DevTools tab. Ctrl + ` (back tick) Execute (single-line command) Enter.

  17. Open terminal via Safari

    Is it possible to open a terminal window from a Safari webpage? I have found a way to open a ssh terminal window, but is it possible to only open the terminal window without pre filled commands? I have tried to search on google but i only find solutions for opening Safari from terminal. To open terminal with ssh from Safari i put the following ...

  18. Opening a URL from the terminal in different spaces macOS

    If I run open https://example.com from the terminal and then switch to another space with a safari window open and run the same command, the safari window in my current space will just get focused and the URL itself will not open.. How can I force the open command to switch me to the other space if that is where the URL is open or just always open it in the current space?

  19. China-Philippines Row Opens New Flashpoint in South China Sea

    Skirmishes between Chinese and Philippine ships in recent days have opened a new flashpoint in the South China Sea, shortly after the two nations struck a deal to ease tensions at another hot spot.

  20. Terminal Open Safari with Website

    Tour Start here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site

  21. How to open terminal from browser in mac?

    ssh:// [email protected]. style URIs so that you can create links that launch terminal. These are simple and clean. At the moment this seems to work for IPv4 addresses, names that resolve to IPv4 or IPv6 addresses, but there doesn't seem to be support for naked IPv6 addresses. If anyone knows the magic runes for the IPv6 It'd be great to know.

  22. Five Charts to Watch in Global Commodities This Week

    Connecting decision makers to a dynamic network of information, people and ideas, Bloomberg quickly and accurately delivers business and financial information, news and insight around the world