Skip to content

This document is a WORK IN PROGRESS.
This is just a quick personal cheat sheet: treat its contents with caution!


Dash

Dash is a small, fast, secure, and POSIX compliant implementation of /bin/sh. Dash is not Bash compatible: be careful of bashisms.

Reference(s)

Table of contents


Install

# emerge -a app-shells/dash
# pacman -S dash

Config

Dash can be optionally defined as the default shell for the system.

In this case, one can identify the scripts containing features of Bash that aren't supported in Dash ('bashisms'), those scripts will not work without being explicitly pointed to /bin/bash.

  • Install checkbashisms:

    # apk add checkbashisms
    

    TODO

    
    

    TODO

    
    

    # emerge -a dev-util/checkbashisms
    
    # nix-env -iA nixos.checkbashisms
    
    # nix-env -iA nixpkgs.checkbashisms
    
    # pacman -S checkbashisms
    

    TODO

    
    

    # xbps-install -S checkbashisms
    

    TODO

    
    

  • Check installed scripts with a #!/bin/sh shebang that may need modification:

    # pacman -S checkbashisms
    $ find /usr/bin/ -type f -perm -o=r -print0 | xargs -0 gawk '/^#!.*( |[/])sh/{printf "%s\0", FILENAME} {nextfile}' | xargs -0 checkbashisms
    $ pacman -Qlq # can be used to list all pacman-installed files.
    

Finally, switch the system default shell:

# su
# eselect sh list
# eselect sh set dash
# exit

# ln -sfT dash /usr/bin/sh

Updates of Bash will overwrite /bin/sh with the default symlink. To prevent this, use the following pacman hook, which will relink /bin/sh after every affected update:
# sudo vi /etc/pacman.d/hooks/dashbinsh.hook
    > [Trigger]
    > Type = Package
    > Operation = Install
    > Operation = Upgrade
    > Target = bash
    >
    > [Action]
    > Description = Re-pointing /bin/sh symlink to dash...
    > When = PostTransaction
    > Exec = /usr/bin/ln -sfT dash /usr/bin/sh
    > Depends = dash
(see https://aur.archlinux.org/cgit/aur.git/tree/dashbinsh.hook?h=dashbinsh)


Use

  • Let's consider the following script:

    $ touch yourscript.whatever
    $ echo "echo hello" > yourscript.whatever
    

    In order to execute it with Dash, run:

    $ dash yourscript.whatever
    

    If you want to execute it directly, then make sure your script starts with #!/bin/dash (or #!/bin/sh if $ ls -la /bin/sh points to /bin/dash), make this script executable ($ chmod +x yourscript.whatever), and run:

    $ ./yourscript.whatever
    

Tip

You can check your scripts for errors, warnings and suggestions with shellcheck.

  • How to create a dash configuration file like $HOME/.bashrc # or ${ZDOTDIR:-${HOME}}/.zshrc or wherever:

    $ cd
    $ touch .dashrc
    $ vi .profile
        > ...
      + > ENV=$HOME/.dashrc; export ENV
    

If this cheat sheet has been useful to you, then please consider leaving a star here.