Linux – OS X terminal – put flags/options at end of command

command linelinuxmacosterminal

Is there a way on OS X to put flags at the end of a command instead of at the beginning? On my Ubuntu box, either of these will work, but OS X treats -la as a directory argument in the second example:

ls -la .
ls . -la

For this simple example, it's not that big of a deal, but it's frustrating when you miss a flag in a long command with lots of options/params and can't just hit up and add the flag to rerun the command.

I'm using Matias' OS X defaults, but I can't see anything in there that would change that behaviour.

Best Answer

This all depends on the program's implementation, or more specifically on how it parses options. OS X does not use the same implementation of ls — it uses the BSD versions thereof, while Ubuntu (and all Linuxes) use the GNU coreutils version.

Its behavior is not something you can change through a setting, but you can install the GNU coreutils through Homebrew:

brew install coreutils

That way, you could do the same thing on OS X. Note that in the default coreutils installation, all GNU utilities will be prefixed with g. So, you'd use gls instead of ls. You can override that behavior by adding them to your path. To do that, add this to your ~/.bash_profile:

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH"

That all being said—and because it all depends on the program implementation—you cannot expect this to work everywhere. Some programs are strict about where options appear, and others are not. It's nothing the OS itself enforces.

What you can also do to quickly add another option is to hit , CtrlA, Option. This will take you to the word after the command name itself, where you can add the option your forgot.

If it doesn't work and prints some weird characters when you press Option, make sure that these keyboard settings are enabled in Terminal.app's preferences under Settings » Keyboard:

Related Question