When I want color in Ubuntu I do

alias ls='ls --color=always'

but on OSX this gives an error ls: illegal option -- - and I need to do

alias ls ='ls -G'

How can I create an alias with the same code and have it work on both systems?


This code works:

ls --color=al > /dev/null 2>&1 && alias ls='ls -F --color=al' || alias ls='ls -GF'

Basically it sees if ls color=al works. If no error (2>&1) use it, else (error) use the other format.

The test_condition && ... || ... is bash shorthand for if test_condition then ... else ...

| improve this answer | |
  • 1
    I suppose your keyboard likes to eat the & :) It was not test_condition &&... || ... ? – Hastur Jun 10 '14 at 19:39
  • 1
    good catch Hastur. Fixed that. – Michael Durrant Jun 10 '14 at 19:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.