Tomas Vik

Bash shell

Script flags

#!/usr/bin/env bash

set -e  # Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
set -u  # Attempt to use undefined variable outputs error message, and forces an exit
# set -x  # Similar to verbose mode (-v), but expands commands
set -o pipefail  # Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.

EOF marker for large verbatim text

read -r -d '' JSON_PAYLOAD <<'EOF'
{
    "prompt_version": 1,
    "project_path": "gitlab-org/gitlab-vscode-extension",
    "project_id": -1,
    "choices_count": 1
}
EOF

echo $JSON_PAYLOAD

Wget

  • Download a file to a folder
    • wget -P /path/to/folder http://example.com/file.tar.gz
      

Directory

  • Get current script directory
    • SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
      

Environment variables

Default value

 export MY_VARIABLE="${MY_VARIABLE:-defaultValue}"

Functions

  • my_func() {
      return "Hello World"
    }
    
    RESULT=$(my_func)
    

If statement

  • if [-a file];then
      ...
    else
      ...
    fi
    
  • Table of all primaries (test command shortcuts) POSIX-Compliant

  • Primary Meaning
    [ -a FILE ] FILE exists.
    [ -b FILE ] FILE exists and is a block-special file.
    [ -c FILE ] FILE exists and is a character-special file.
    [ -d FILE ] FILE exists and is a directory.
    [ -e FILE ] FILE exists.
    [ -f FILE ] FILE exists and is a regular file.
    [ -g FILE ] FILE exists and its SGID bit is set.
    [ -h FILE ] FILE exists and is a symbolic link.
    [ -k FILE ] FILE exists and its sticky bit is set.
    [ -p FILE ] FILE exists and is a named pipe (FIFO).
    [ -r FILE ] FILE exists and is readable.
    [ -s FILE ] FILE exists and has a size greater than zero.
    [ -t FD ] file descriptor FD is open and refers to a terminal.
    [ -u FILE ] FILE exists and its SUID (set user ID) bit is set.
    [ -w FILE ] FILE exists and is writable.
    [ -x FILE ] FILE exists and is executable.
    [ -O FILE ] FILE exists and is owned by the effective user ID.
    [ -G FILE ] FILE exists and is owned by the effective group ID.
    [ -L FILE ] FILE exists and is a symbolic link.
    [ -N FILE ] FILE exists and has been modified since it was last read.
    [ -S FILE ] FILE exists and is a socket.
    [ FILE1 -nt FILE2 ] FILE1 has been changed more recently than FILE2, or if FILE1 exists and FILE2 does not.
    [ FILE1 -ot FILE2 ] FILE1 is older than FILE2, or is FILE2 exists and FILE1 does not.
    [ FILE1 -ef FILE2 ] FILE1 and FILE2 refer to the same device and inode numbers.
    [ -o OPTIONNAME ] shell option “OPTIONNAME” is enabled.
    [[ STRING =~ ^regex$ ]] STRING matches regex
    [ -z STRING ] the length of “STRING” is zero.
    [ -n STRING ] or [ STRING ] the length of “STRING” is non-zero.
    [ STRING1 == STRING2 ] the strings are equal. “=” may be used instead of “==” for strict POSIX compliance.
    [ STRING1 != STRING2 ] the strings are not equal.
    [ STRING1 < STRING2 ] “STRING1” sorts before “STRING2” lexicographically in the current locale.
    [ STRING1 > STRING2 ] “STRING1” sorts after “STRING2” lexicographically in the current locale.
    [ ARG1 OP ARG2 ] “OP” is one of -eq, -ne, -lt, -le, -gt or -ge. *
    • * These arithmetic binary operators return true if “ARG1” is equal to, not equal to, less than, less than or equal to, greater than, or greater than or equal to “ARG2”, respectively. “ARG1” and “ARG2” are integers.
    • Source: Introduction to if
  • Table of primaries - non-posix complient (using [[ ]])

    Primary Meaning
    [[ STRING =~ ^regex$ ]] STRING matches regex
    [[ STRING != *"substring"* ]] STRING doesn’t contain substring
  • If a command returns 0, it’s considered TRUE