Applescript: Run or Call a Shell Script

How do run a shell script with an AppleScript? How do I integrate shell scripts into AppleScript? How do I call a shell script called /path/to/ using an applescript?

AppleScript is a scripting for Mac OS. It is the Mac OS scripting interface, which is meant to operate in parallel with the graphical user interface. With AppleScript, you can control, and communicate among, applications, databases, networks, Web services, and even the operating system itself.

Running Shell Commands From AppleScript Scripts

You can easily execute shell commands from your AppleScript using the following syntax:

do shell script "command"
do shell script "command1; command2"
set variableName to do shell script "command"
set variableName to do shell script "command1; command2"
do shell script "/path/to/"
do shell script "/bin/tcsh /path/to/yourscript.csh"
do shell script "/bin/tcsh -c  'command1'"

The following script statement uses a do shell script command to get list of the files from the current directory and store it into the AppleScript variable filesLists:

set filesLists to do shell script "ls"

Task: Pass an AppleScript Variable To Shell Command

Use the following syntax (h is an AppleScript variable which is passed to traceroute shell command):

set h  to ""
do shell script "traceroute " & h

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 7 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
7 comments… add one
  • mike Jul 16, 2011 @ 5:38

    Thanks, now I want to simply copy files from the same dir as the bash script. Applescript and also *.command mess the current directory so the files won’t get found. Users can run it the classic way ./bashscript but I want it clickable in whatever way.

  • sid Jul 18, 2011 @ 5:46


    I have the same problem as above.
    I want to create an apple script which runs a pre written shell script (.sh) file.

    my apple script file and .sh file are in the same folder.
    I have written the following line in apple script file.

    do shell script “sh”

    so it gives me following error.
    error “sh: No such file or directory” number 127

    please guide me whatever is missing.

    • Pierre Thibault Nov 1, 2016 @ 5:07

      do shell script “pwd” gives me “/” so I guess you have to assume that the default directory is the root of the file system.

  • byron Jan 2, 2012 @ 22:27

    I do what you say, but my script is not able to stop, and it doesn’t work here is my shell script while (true) do echo -n ” GO BADGERS “; sleep .008; done

  • staid Jun 17, 2012 @ 2:54

    You said about passing applescript variable to shell. But now I get a problem to passing shell variable to applescript. How can I do this?

    • bug Sep 7, 2012 @ 20:32

      set shellvar to do shell script “echo ${varname}”

  • John Stewart Jan 15, 2017 @ 14:44

    could we please change the variable name in the traceroute example to the same one being set one line above? while my problem is laziness in copy/paste and poor eyesight visually differentiating between h and n, applescript seems to be insensitive to this shortcoming on my part and mocks me with an unset variable error. novices in scripting/programming &c may see the error and imagine some non-existent correlation of h and n forever hazing their understanding or giving into abandoning the endeavour.

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum