Shell

Choices

  • SH - for maximum compatibility
  • Bash (Bourne Again Shell)
    • Traditional, widely available.
    • (Relatively) slow.
    • Lacking in interactive features.
  • Zsh
    • Default on macOS.
    • Modern, feature rich.
    • More powerful scripting syntax than Bash.
  • Fish
    • User-friendly, not POSIX compliant, more ergonomic.
    • Best for interactive use.
  • Nushell, similar to PowerShell, everything is an object. Could be helpful in batch processing things.
  • Fast shells
    • Dash
      • “Fastest” and minimal.
      • Focused on POSIX compliance, not interactive use.
      • Best for non-interactive scripts.
    • Ion Shell, written in Rust, claims to be faster than dash

INFO

The benchmark on my MacBook Pro M3 Pro demonstrates that fish is the fastest overall, even compared to dash and mksh.

Snippets

# Batch copy and substitute file name.
for f in good*.bminor; do cp "$f" "${f//good/bad}"; done
 
{ cmd1; cmd2; cmd3 } > /dev/null  # group stdout of commands
cat ... | head -n -5 | tail -n +5  # exclude 5 lines at begining or end
tail -f file  # watch the tail of a file
\ls  # precede command with backslash to suppress aliases
pstree; vmstat; tload
set  # prints all variables
env; printenv  # prints only env vars
  • Use Alt/Opt-F and Alt/opt-B to go forward/backward a word!
  • Many other Emacs keybinding can be used as well.
  • Pressing and releasing the Esc key has the same effect as holding down Alt key (on a real terminal)
  • Alt-? displays all possible completions, Alt-* inserts all possible completions.
  • -option is called “BSD style”!
  • Login shells read profiles, non-login shells read rc files. Non-login shells also inherit env vars from login-shells.
  • Shell variables are not inherited, env variables are.

Other Concepts

  • ANSI Escape Code
    • 033 (the code generated by ESC), optional character attribute, and an instruction.
    • \033[0;30m (black) and \033[1;30m (dark grey), the 0 and 1 are bolt attribute.
    • Non-printing characters should be wrapped in \[\].
    • Escape codes can also be used to move cursor around.
  • .bash_profile is only used for login shells. .bashrc is used for interactive shells.

Shell Prompt

(Modern) Tools

  • Z, learns most used directories for cd. There’s port for Fish.
  • Zoxide is a faster and more feature rich alternative to z.
  • ouch, a user-friendly archival utility.
  • dust, duf, Rust implementations for du and df.
  • lsd, “ls Deluxe”