11

I need to do a few little scripts on my Mac but I don't really want to install Xcode or Python for a full environment, just a quick command line deal. Does macOS come with anything suitable out of the box?

As an example I want to process files in a directory and do some custom logic.

  • 3
    Not strictly from Terminal, but Automator, Script editor ? – anki Nov 5 '19 at 19:11
  • 13
    Python already is installed. – nohillside Nov 6 '19 at 6:15
  • 3
    Best not to count on those scripting languages always being there: apple.stackexchange.com/a/361842/14499 – kojiro Nov 6 '19 at 11:37
  • @kojiro There are so many shell scripts required to boot and run macOS, I doubt that Apple will remove any shell anytime soon. Other scripting languages like python or ruby can always get installed via Homebrew – nohillside Nov 7 '19 at 13:21
  • @nohillside I wasn't referring to sh or ancient versions of bash. I think macOS will remain at least somewhat POSIX compliant for the foreseeable future. – kojiro Nov 7 '19 at 14:12
25

Terminal runs the Unix shell, (bash, zsh, and others) which is obviously a command line scripting environment, which can execute complex scripts, particularly file processing.

Python 2.7 comes bundled with MacOS, and always has done. Future?.

Ruby, Perl are also included.

AppleScript and its JavaScript equivalent (docs) are also there out-of-the box.

| improve this answer | |
  • 2
    "its JavaScript equivalent" - what do you mean by that? – d-b Nov 6 '19 at 6:06
  • I believe gcc is also installed. But it's been a while since I used a Mac so I'm not sure. – Justin Nov 6 '19 at 6:29
  • 2
    @JustinLardinois it is not. – xrisk Nov 6 '19 at 6:31
  • 1
    @d-b there‚Äôs a JavaScript frontend to the same Open Scripting Architecture that AppleScript is built on. The Script Editor app and osascript command line tool let you switch between them. – rickster Nov 6 '19 at 6:32
  • @d-b Added docs to the answer. – anki Nov 6 '19 at 11:43
26

You have good old bash (or csh, or whatever). You can use it to write shell scripts.

| improve this answer | |
  • 3
    Quite so. They can also make them easy to run see - How to run a shell script in OS X by double-clicking? – lx07 Nov 5 '19 at 19:09
  • 1
    bash (or zsh in Catalina) plus any of the commands that the system comes with. A lot of the operations you might want to perform can be done through other utilities (cut, grep, sed, awk, xargs, etc.) Searching for bash one liners may help discover how different tools can be chained together to achieve what you are looking for. – Nate Nov 5 '19 at 20:25
  • I never really got into bash though I did back together batch file 'programs' back in the day. How does bash compare in terms of writing simple programs? – Mr. Boy Nov 7 '19 at 0:17
  • 1
    It's very powerful. It can deal with functions, variables etc., more or less everything that can be done in a 'traditional' programming language (like C). Check out a post exploitation toolkit that is written in bash, for example: github.com/zMarch/Orc – FiddleDeDee Nov 7 '19 at 13:09
  • @FiddleDeDee ok that's good to know. I've used enough languages that one more shouldn't be hard to pick up – Mr. Boy Nov 7 '19 at 20:08
9

I'm on Mojave and Python, Ruby, and Perl are installed by default. But I understand that there is a Deprecation notice with Catalina that says future versions of macOS won't include scripting languages by default.

As suggested, as a clarification there are Unix shells in macOS, although in Catalina the default shell is zsh.

Testing on a 10.5.8, El Capitan and a Mojave computer - perl, ruby and python responded on the command line without providing any paths. Perl and ruby are in /usr/bin/ on each machine.

| improve this answer | |
  • 3
    They intend to remove all scripting languages from the default install? Surely at least Bash will stay, no? And I'm surprised if they remove Python, too. – leftaroundabout Nov 6 '19 at 10:30
  • apple.stackexchange.com/a/361842/14499 for more information – kojiro Nov 6 '19 at 11:38
  • 2
    My understanding is that there will still be Unix shells, although in Catalina the default shell is zsh, see support.apple.com/en-us/HT208050 – David Rouse Nov 6 '19 at 12:06
  • You might edit this answer to clarify. Also thanks for pointing out ruby and Perl... Do they run right from terminal ie are paths etc preset by default? – Mr. Boy Nov 7 '19 at 0:22
4

In addition to the languages already mentioned in other answers, macOS Mojave also comes with the general purpose programming language PHP, which is well suited to processing a number of files in a directory and performing custom logic.

In addition, macOS Mojave also comes with the programming languages awk and m4 that are specifically suited for processing text files in this manner.

If you're also looking at compiled languages, it is possible to use C, C++ and Objective-C from the command line without installing the whole XCode. You only need to install the Xcode Command Line Tools. In that same package you'll find the "swift" command that allows you to run scripts in the Swift programming language without having to compile them first.

| improve this answer | |
  • CLT has also Swift programming language. The version for Catalina has also Python 3.7.3. – Yoan Nov 7 '19 at 13:45
  • @Yoan I have added a sentence explaining that Swift is also a possibility - without (manually) compiling first. Yes, Catalina has other options - but this question was asked about Mojave. – jksoegaard Nov 7 '19 at 14:04

You must log in to answer this question.

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