How does Mac's command line compare to Linux?

29

12

I love Ubuntu Linux - especially the commmand line. But I have to admit that, at least for now, Windows is more user-friendly - there's more software for it, more drivers, and more stuff just works.

Knowing that Mac is built on Unix makes me wonder if it's the sweet spot between them. But I wonder: how similar is the Mac command line to Linux's bash? Could I pick right up with using vim and bash scripting and git, etc? Would common commands like changing directories be different? Does anybody know an online "compare and contrast" resource?

Nathan Long

Posted 2010-08-23T10:39:33.267

Reputation: 20 371

See also https://unix.stackexchange.com/questions/82244/bash-in-linux-v-s-mac-os

– Cees Timmerman – 2019-04-15T15:46:24.823

Answers

13

Bash is bash so the shell and its built-ins are the same. But be aware that the behavior of some tools like ps or head can differ in the details thus your scripts might fail.

user16115

Posted 2010-08-23T10:39:33.267

Reputation:

2You can standardize on the gnu binutils toolset across linux and OSX. Macports and homebrew will both install on OSX. This will at least standardize things like ls/tar/etc. ps is still OS dependent. – skarface – 2015-02-17T22:19:41.390

5Bear in mind that this isn't so much a Mac vs. Linux problem, rather than a various flavors of unix do things slightly different problem. You would find the same (actually more differences) between Linux and, for example, HP-UX. – KeithB – 2010-08-23T13:08:04.787

also file: file -i on linux while file -I in osx ☹️ – Anthony Kong – 2019-06-07T23:14:08.600

31

Mac OS X is a Unix OS and its command line is 99.9% the same as any Linux distribution. bash is your default shell and you can compile all of the same programs and utilities. There's no notable difference. You can also find various projects like MacPorts which provide package management for Mac.

w0rm

Posted 2010-08-23T10:39:33.267

Reputation: 327

also take as example 'date' command; far not the same. eg:

date "+%Y-%m-%d %H:%M:%S" --date "2014-05-06 05:05:05"

which on OSx doesnt work – Diego Andrés Díaz Espinoza – 2014-07-11T16:19:43.210

1I frequently use the commands cat and cal. Imagine my surprise, to discover that on a mac they accept different parameters. 99.9% is way off, when I have to consult a manual to use basic utilities like cat or find. – Benubird – 2015-07-27T13:59:02.973

1+1: the above is true. You may need to bear in mind weather your mac uses an intel or a powerpc processor when it comes to compiling code or using pre-built packages, i understand new macs are only intel but i may be wrong.

typo @ w0rm: package* management. :-) – Sirex – 2010-08-23T10:54:29.330

2Although your scripts will probably work for both Mac and Linux, it's always wise to test them on both platforms. I've seen quite some (subtle) differences between linux distributions and even between different versions of the same linux version. – Roalt – 2010-08-23T11:07:22.490

1FeeBSD to be exact. – digitxp – 2010-08-23T18:27:22.573

6@digit, that must be the BSD version they charge for? s/F/Fr/ ;) – John T – 2010-08-23T18:30:34.253

Indeed, this is an example of 100% bogus statistics. Depending on your use case the range is off the cuff something like 70%-100% but attempting to quantify this as a percentage is really just silly. – tripleee – 2018-03-26T07:53:35.977

Another thing I've discovered is that some programs one uses from the command line have small differences. For example, GNU find supports an exec option, but the default find on OSX doesn't. – Nathan Long – 2012-08-15T22:51:30.947

@NathanLong OS X's find should support -exec, but not -printf. – Lri – 2012-09-19T12:44:16.083

1899.9% is not correct. OS X is a BSD variant and does not have GNU utilities. You can always install them but an a lot of commands have different options. It's Unix but not Linux. – Matteo – 2013-05-10T18:50:15.667

@Matteo - good point. I've found it helpful to check man pages to see if something is the same on Mac/Linux. Eg, run man sort on two systems and look at the attribution. – Nathan Long – 2014-03-10T19:10:47.597

12

It's very similar, but if you know of any BSD v. Linux differences, expect the Mac to have the BSD behavior.

Neth

Posted 2010-08-23T10:39:33.267

Reputation: 475

Interesting. Can you elaborate more on that? Why is that? – fat_mike – 2016-11-27T16:16:20.910

@fat_milke because OSX uses the BSD utilities not GNU which Linix uses – user151019 – 2016-12-30T19:17:46.103

b/c the Mac OSX derived from a *nix kernel (NeXTSTEP based on BSD) that branched off of Unix earlier than Linux. Thus, they ended up w/ slightly different implementations of some utils. See https://en.wikipedia.org/wiki/MacOS

– Nietzsche – 2019-08-30T03:49:06.653

Also see https://en.wikipedia.org/wiki/MacOS#/media/File:Unix_timeline.en.svg

– Nietzsche – 2019-08-30T04:48:52.433

9

A couple things - it really depends on what you mean by command line.

  1. You'll probably run bash, which will be the same as bash on any other UNIX. The bash programming language will be the same too - as long as it's the same version. Bash versions have slight differences in the language, but this has nothing to do with MacOSX vs. Linux per se.

You may actually bump into this - /bin/bash on a Mac is pinned to 3.x for licensing reasons. You may find minor compatibility issues if you’re used to 4 or even 5.

  1. bash by itself is a small subset of 'command line'. To be useful you typically use some other common UNIX utilities such as awk, or ls, or cut. Because of its history (it's descended from BSD, not Linux or SystemV), the Mac versions of utilities will be different than the ones you're used to on Linux (a.k.a. GNU versions). These will have at least some flag differences, and some output and functionality differences than Linux. Scripts that use these will have to be changed to compensate. BSD awk vs GNU gawk is pretty different. OR, you can get these GNU versions from macports or home brew, and set your PATH so that these are the defaults (over the built in versions).

  2. MacOS X also has some new cool tools. open is nice, it will open the object as if you double clicked it. defaults allows you to set some preferences, some not even in the GUI preferences panels. textutil can be useful and say can be fun.

Rich Homolka

Posted 2010-08-23T10:39:33.267

Reputation: 27 121

3

The command line functionality on Mac and Linux are indeed similar as OS X has a flavour of UNIX (called Darwin) underneath the GUI that you see.

The default shell on OS X is bash, so if you're familiar with that you will adjust nicely.

On a Mac, the default command line application is Terminal. There are also a number of other emulators out there but overall Terminal provides a very similar look and feel to the shell you are used to in Linux.

The primary source of differences though are:

  • Different command line arguments at times (i.e. see du for example). The primary commands like cd or ls etc. are similar.
  • There are more parts of OS X that shouldn't be edited by hand as you might do in Linux. This is a topic of its own, but just be aware that running a Mac is not exactly like running a Linux box in terms of configuration and upkeep.

That being said there are also a lot of cool hacks available from the command line in OS X (e.g. open commnand). There are lots of previous threads and blog posts out there with more info.

You can also get your hands on a bunch of opensource packages (i.e. git, hg, etc.) via MacPorts, Fink or a few other package management systems for OS X. You can also build other source packages intended for UNIX/Linux like systems as necessary.

dtlussier

Posted 2010-08-23T10:39:33.267

Reputation: 1 985

0

Here is one difference. Try this command in both Mac and Linux bash shell

find /etc -name "*.conf"

Mac displays nothing, even though there are lot of .conf file inside /etc.

Antony Thomas

Posted 2010-08-23T10:39:33.267

Reputation: 427

3Maybe you could tell people why this is. – Mads – 2012-08-28T13:58:04.447

1Not sure, maybe it has got to do with the pathname expansions in POSIX. This command works find /etc/* -name "*.conf" . Nevertheless, that was provided as an example to contrast MAC command line to LINUX command line. Cannot reason a -ve vote. – Antony Thomas – 2012-08-29T04:42:32.053

4That's because /etc on OSX is a symbolic link. If you try find /etc/ -name "*.conf" it indicates /etc is a directory instead of a file. Does find in Linux treat symbolic links to directories as directories by default? – MechEthan – 2012-11-08T17:13:25.797