19

I was following this article to update my bash. I really want to update my version of bash so that I can use the --group-directories-first option for the ls command.

So far I have accomplished the following from the linked article:

  • I successfully executed the brew install bash command and downloaded the new bash. Per the article I can verify that the the /usr/local/bin/bash --version on my machine shows the following:

    GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

    This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

    • I completed the sudo vim /etc/shells command, editing the file to include the new downloaded bash. This is what it looks like:

enter image description here

  • I executed the chsh -s /usr/local/bin/bash to set the new bash as default.

However, even after I close the terminal and relaunch it, I am not sure if I am using the new terminal. This is because:

  1. when I execute bash --version I get the following:

    GNU bash, version 5.0.11(1)-release (x86_64-apple-darwin18.6.0) Copyright (C) 2019 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

    This is free software; you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

But when I run echo $BASH_VERSION I get something to the contrary (3.2.57(1)-release). Isn't that the old verison?

  1. Also, the new "group directories first" command still doesn't work. I get the following error when I run ls --group-directories-first:

ls: illegal option -- - usage: ls [-ABCFGHLOPRSTUWabcdefghiklmnopqrstuwx1] [file ...]

Any help or pointers would be greatly appreciated.

Thanks!

pynexj
  • 19,215
  • 5
  • 38
  • 56
im2wddrf
  • 551
  • 2
  • 5
  • 19
  • could you tell the output of `echo $SHELL` – Mihir Luthra Sep 17 '19 at 10:35
  • @Mihir I got as the output `/bin/bash`. Does this mean I missed a step in my configuration? – im2wddrf Sep 17 '19 at 10:37
  • Not really. I did the same steps (except I use macports as the package manager) and it works fine for me. – Mihir Luthra Sep 17 '19 at 10:40
  • 5
    `ls` is not part of `bash`. `ls` is in [Coreutils - GNU core utilities](https://www.gnu.org/software/coreutils/). – pynexj Sep 17 '19 at 10:40
  • @pynexj then perhaps this may be a misunderstanding on my part. Would it be possible to update my machine so that I am using the new features of `ls`? Not sure what exactly I would update in this case. – im2wddrf Sep 17 '19 at 10:43
  • ah completely forgot that, `ls` is a separate utility, not a part of `bash`. – Mihir Luthra Sep 17 '19 at 10:44
  • @Mihir I found this article (https://clubmate.fi/upgrade-to-bash-4-in-mac-os-x/). It more or less describes what I see (difference between `--version` and `$BASH_VERSION`) but I am not sure how their solution addresses my issue. Should that discrepancy be of any concern at this point? – im2wddrf Sep 17 '19 at 10:45
  • 2
    try `brew install coreutils`. commands installed with `brew` may have added the prefix `g`. e.g. `ls` vs. `gls`, `find` vs. `gfind`. – pynexj Sep 17 '19 at 10:45
  • @pynexj I have just now brewed installed `coreutils`. My `ls --group-directories-first` still doesn't work. Is there an extra step needed after I install it? I have already tried shutting down / quitting the terminal and relaunching it. – im2wddrf Sep 17 '19 at 10:48
  • 2
    Isn't this more a question for https://unix.stackexchange.com/? – Bayou Sep 17 '19 at 10:49
  • @pynexj Thank you ! Looks like it works now. My issue has been solved. You are correct that there is a prefix g for the command. I've lost my custom color output though (I had my alias ls refer to `ls -Gla` where my colors were configured through LSCOLORS). Thanks again for the help—I appreciate it. – im2wddrf Sep 17 '19 at 10:58
  • @im2wddrf, not expd with `brew`, making a guess you can find the location of coreutils by `brew info coreutils` and then find `ls` there, idk if `brew` already adds it to path. but in any case, you can add it to path by urself. – Mihir Luthra Sep 17 '19 at 10:59
  • @im2wddrf gnu ls has --color=auto – pynexj Sep 17 '19 at 11:00
  • @pynexj right. I have the following in my bash profile `alias ls='gls -Gla --color=auto --group-directories-first'`. When I tinker with my LSCOLORS it doesn't really change the color of the directory, just defaults to blue. – im2wddrf Sep 17 '19 at 11:02
  • @im2wddrf `gls` doesn't use `LSCOLORS`; it uses `LS_COLORS`. Please read the documentation for `gls`. – chepner Sep 17 '19 at 11:56

1 Answers1

48

This has nothing to do with bash. Command ls is not a builtin of bash.

macOS is based on the Unix operating system. Some of its basic commands are BSD flavored. The --group-directories-first option you want is available in GNU ls only. All you need is a GNU flavored ls.

Solution: install coreutils, in which GNU ls is included.

brew install coreutils

Add the following code into ~/.bash_profile, to prioritize the ls command and other GNU flavored commands over the builtin BSD flavored command in macOS.

# Make all GNU flavor commands available, may override same-name BSD flavor commands
# For x86 Mac
export PATH="/usr/local/opt/coreutils/libexec/gnubin:${PATH}"
export MANPATH="/usr/local/opt/coreutils/libexec/gnuman:${MANPATH}"

# For M1 Mac
export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:${PATH}"
export MANPATH="/opt/homebrew/opt/coreutils/libexec/gnuman:${MANPATH}"

If you only want the GNU ls but not other GNU flavored commands. Don't add the above content, but create an alias in ~/.bash_profile.

# For x86 Mac
alias ls="/usr/local/opt/coreutils/libexec/gnubin/ls"

# For M1 Mac
alias ls="/opt/homebrew/opt/coreutils/libexec/gnubin/ls"

Here's all commands brought by coreutils.

❯ ls /usr/local/opt/coreutils/libexec/gnubin
'['         cat      cksum    dd          echo     fmt      install   ls       mv       od         printf     rmdir       sha384sum   split    tac       tr         unexpand   wc
 b2sum      chcon    comm     df          env      fold     join      md5sum   nice     paste      ptx        runcon      sha512sum   stat     tail      true       uniq       who
 base32     chgrp    cp       dir         expand   groups   kill      mkdir    nl       pathchk    pwd        seq         shred       stdbuf   tee       truncate   unlink     whoami
 base64     chmod    csplit   dircolors   expr     head     link      mkfifo   nohup    pinky      readlink   sha1sum     shuf        stty     test      tsort      uptime     yes
 basename   chown    cut      dirname     factor   hostid   ln        mknod    nproc    pr         realpath   sha224sum   sleep       sum      timeout   tty        users
 basenc     chroot   date     du          false    id       logname   mktemp   numfmt   printenv   rm         sha256sum   sort        sync     touch     uname      vdir
Simba
  • 23,537
  • 7
  • 64
  • 76
  • 1
    Thank you so much! I will definitely consider adding that to my bash profile to see if the newer commands are of use. Thanks! – im2wddrf Sep 17 '19 at 19:45