Skip to content
GradedJestRisk edited this page Jul 28, 2021 · 14 revisions

Table of Contents

Shells

List:

Which shell

List:

  • get current interactive shell:
    • echo $0
    • ps --pid "$$"
  • get parent shells: ps -o pid,cmd --forest
$ ps -o pid,cmd  --forest
    PID CMD
   5357 /usr/bin/zsh
  17522  \_ zsh -i
  20461      \_ bash
  20781          \_ ps -o pid,cmd --forest
  18106 zsh -i

Configuration

  • get installed shells cat /etc/shells
  • get default non-interactive shell ls -ltr /bin/sh
  • get default interactive shell: echo "$SHELL"
  • change default shell: chsh

Mode

Shell can run in:

  • interactive mode, so that user can tell dynamically which script to run: bash -i
  • non-interactive mode, to execute a single script (which can ask for user input): bash -c 'echo Hello, world!'
Each shell runs in a process
❯ printf "Process %d is running %s" $$ $0
Process 5357 is running zsh%    

❯ zsh -i
❯ printf "Process %d is running %s" $$ $0
Process 17522 is running zsh%                                                                                          ❯ echo $@

❯ bash -c 'printf "Process %d is running %s" $$ $0
Process 15338 is running bash%   

Portability

POSIX is a standard for shell. dash aims at POSIX-compliance. To check your script compatibility, run your script with shellcheck shellcheck --shell=dash ./myscript.sh

Unfortunately, 'portable' is usually a stronger requirement than 'POSIX-compliant' for shell scripts. That is, writing something that runs on any POSIX shell isn't too hard, but getting it to run on any real-world shell is harder.
From here

POSIX non-compliant: - fish

Test

Test framework for shell:

craft

List:

Shortcut

Shortcut with Ctrl:

  • cancel Y
  • interrupt current command C
  • clear the line
    • up to the beginning: U
    • forward (right): K
    • downward (left): W
    • several strokes: E U / A K
  • go to
    • beginning of the line: A
    • end of the line: E
  • clear screen (prompt): L
Clone this wiki locally