​​ ​

The Apple Support Community will permanently close on November 20, 2026. Starting today, the site will no longer accept new posts and is available in a read-only state.


You can continue to find trusted guidance and support on the official Apple Support site. 


We want to thank the community members who have contributed their time and expertise to help others with their Apple products.


How make bash script using osascript to close app from command line

When executed in Terminal, a command like the following will close the named, open, app:


osascript -e 'quit app "BBedit"'


I created the following bash script, in file closeapp.sh and made it executable (chmod a+x closeapp.sh).


#! /bin/bash
# gracefully close app 
osascript -e 'quit app "$1"'


If I try it...


closeapp.sh BBedit


... then nothing happens.


What's wrong?

Posted on Jul 29, 2020 8:47 AM

Similar questions

7 replies

Jul 29, 2020 10:00 AM in response to murrayE

The pkill command is a compiled binary that directly sends a SIGTERM (-15) signal to a process name that you provide to the script. Can be done from the command-line without the need of a script.


With osascript, you are asking it to interpret, and execute the AppleScript command syntax that tells the operating system to do the same thing as pkill. Far more overhead using osascript.


Your choice of which you use.

Jul 29, 2020 9:22 AM in response to murrayE

osascript is very complicated with shell variables because AppleScript uses double quotes and single quotes.

You have to escape the quotes:

#!/bin/bash
# gracefully close many apps 
for a in "$@"
do
	osascript -e 'quit app "'"$a"'"'
done

See here for more info and how to prevent command line injection: https://stackoverflow.com/questions/23923017/osascript-using-bash-variable-with-a-space/23923167#23923167

Jul 29, 2020 12:47 PM in response to murrayE

The command-line argument must be the identical spelling of the running application name. It must be BBEdit, not bbedit, or any other spelling because pkill is matching on the identical string in the process table.


The same code as you posted with the BBEdit command-line argument works perfectly to kill BBEdit when run here.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

How make bash script using osascript to close app from command line

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.