Skip to content

Bash

General

  1. Standard streams

    • stdin (0): standard input
    • stdout (1): standard output
    • stderr (2): standard error
  2. Redirections

    • >: redirect stdout
    • 2>: redirect stderr
    • 2>&1: redirect stderr to stdout

Tips and Tricks

Copy and create destination dir if it does not exist

There is no direct option for cpdirectly, mkdir needs to be used in combination.

sh
# Exemple utility function
cpd() {
  mkdir -p "$(dirname "$2")" && cp "$1" "$2"
}

See Stack Overflow thread

Git output to stderr even if successful

console output sent to stderr when it should be stdout

From ChatGPT:

Git Commands and Stderr: Some git commands, like git checkout and git pull, may write output to stderr as part of their normal operation, even if they succeed. This output may include things like status messages, warnings, or just notifications (like switching branches). These messages aren't necessarily errors, but they are still sent to stderr.

Dirty workaround:

bash
git checkout 2>&1
git pull 2>&1
git push 2>&1