macOS - Best Way to Command Line Practice Text Processing?

I’d like to start doing some text processing (awk, grep, sed, etc.) with the command line on my computer (Mac). The default shell is Zsh but it is possible to change it to Bash although I’ve read that the Mac OS is installed with an older version of Bash by default.

I’m wondering if I should just practice with Zsh or if I should use Bash?

Thank you!

Start up a specific shell

From uses of exec command in shell:

Whenever we run any command in a Bash shell, a subshell is created by default, and a new child process is spawned (forked) to execute the command. When using exec, however, the command following exec replaces the current shell. This means no subshell is created and the current process is replaced with this new command.

General command to start up a new shell with no subshell process

exec $SHELL

where $SHELL represent the path to the shell

On terminal, type and enter the following to run a new bash shell:

exec bash

or you change back to a new zsh shell by

exec zsh

Upgrade bash in macOS tutorial:

Due to licensing issues, macOS shows a bash version 3.2.

$> bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.

Newer versions of macOS (Starting from Catalina) now users zsh as default instead of bash.

Install the required homebrew

/usr/bin/ruby -e "$(curl -fsSL"

Install bash updated version via homebrew

brew install bash

Reload bash into a new shell

exec bash

:warning: Be careful: Your old bash 3 version would still be installed in /bin/bash while the brew version would be in /usr/local/bin/bash . You can check which version you are using with echo $SHELL or which bash .

Check bash version

$> bash --version
GNU bash, version 5.1.4(1)-release (x86_64-apple-darwin20.2.0)
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>

Bash version not updated

If you do not see the updated version, your path environment variable might need some rework.

Reload bash with full path into a new shell

exec $(echo $(brew --prefix)/bin/bash)  

Change default bash on macOS

First, update the list of permitted shell by adding the bash brew version into /private/etc/shells.

$> echo $(brew --prefix)/bin/bash | sudo tee -a /private/etc/shells

To check /usr/local/bin/bash has been added to /private/etc/shells

$> cat /private/etc/shells
# List of acceptable shells for chpass(1).
# Ftpd will not allow users to connect who are not using
# one of these shells.


Finally, update your user’s shell with chpass command line.

$> sudo chpass -s /usr/local/bin/bash alvin
Changing shell for alvin.

Alternatively, instead of using chpass , you can go to the Menu > System Preferences... > Users & Groups . Unlock the pane, control click on your user to select Advanced Options... , then update the Login shell to /usr/local/bin/bash

Use correct command utilities program and documentation

There’s a big differences in how a command is used based on either Linux (GNU) or macOS (BSD) version of the command utilities.

awk, grep, sed in macOS most likely are BSD version.

In most tutorial you find online including DataQuest are based on GNU command utilities.

Regardless of whether you used BSD or GNU command utilities, ensure you use the right (BSD or GNU) documentation.

Install GNU core command line utilities on macOS

Guide to install GNU core utilities:

brew install coreutils findutils gnu-tar gnu-sed gawk gnutls gnu-indent gnu-getopt grep

Selection of shell

There’s no best selection of shell choice. All about personal preferences.

Personally, I used bash because most OS and online tutorials uses Bash.

bash setting on macOS

For bash, you need to have the following files on macOS:

  • ~/.bashrc to initialize the bash interactive shell session.
  • ~/.bash_profile to load the path variables
  • ~/.profile for non-interactive shell session for commands

Further reads on why Bash is the most default shell

You can read up on Why is bash the default shell in most OS?


Thank you @alvinctk for all this information!

1 Like