2

I'm new to OS X and also other Linux distributions(Ubuntu, CentOS, RHEL) user.

I want to know why OS X's bash can not use --color=auto option and how to enable it.

I often use ls --color=auto, but on OS X, it doesn't work. The following is the command output:

$ ls --color=auto
ls: illegal option -- -
usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

I also read man page of ls, and I found ls -G is enabler of colorized output. So, at this time, it's okay, but I'm a little bit annoying because I'm sharing the .bashrc and .bash_profile for all my linux environments.

Does anyone know these bash's different? And do you have any good idea to share the .bashrc and .bash_profile between OS X and some linux distributions without additional edit on each environment.

P.S. My friend tells me bash on AIX(Linux server IBM version? I'm not sure) could not run ls --color=auto also.

tkhm
  • 860
  • 2
  • 10
  • 30
  • 3
    [This answer](http://stackoverflow.com/a/394247/3591528) uses os detection to alias color `ls`. – teppic Feb 07 '17 at 03:07
  • 1
    ls on macOS isn't bash, it's a command from a derivative of BSD UNIX. – clearlight Feb 07 '17 at 03:07
  • @teppic I also use ls alias, but did not know `$platform` variable, thank you! – tkhm Feb 07 '17 at 05:23
  • @clearlight I tried to run `ps -p $$` and I got `47232 ttys000 0:00.45 -bash`, but you mean it's not bash itself, it is a command from that?! Thank you give me a clue, I'll search and check with it. – tkhm Feb 07 '17 at 05:29

2 Answers2

8

The implementation of ls command and other commands like ps and top is different, most utils have a GNU version and BSD version. Linux take the GNU version while OSX may take a BSD version, so options for those commands may differ.

if you want to make a .bashrc or .bash_profile that works everywhere, you should judge the environment before you alias your command like:

_myos="$(uname)"

case $_myos in
  Linux) alias ls='ls --color';;
  Darwin) alias ls='ls -G';;
  *) ;;
esac
Grisha Levit
  • 8,194
  • 2
  • 38
  • 53
armnotstrong
  • 8,605
  • 16
  • 65
  • 130
  • Thank you for your answer. I ran the command and it says `Darwin`! I'll add this for my `.bashrc`. Thank you. – tkhm Feb 07 '17 at 05:32
  • so basically a variation of the answer at the link provided by @teppic as the first comment reply to the question. – clearlight Feb 07 '17 at 05:32
0

Use homebrew to install GNU coreutils

brew install coreutils
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"

useful link:https://github.com/sorin-ionescu/prezto/issues/966

zhanhezou
  • 11
  • 1