Gam Command Cheat Sheet

  

Look no further than these excellent cheat sheets by data practitioners Dr. Tim Essam and Dr. These compact yet well-organized sheets cover everything you need, from syntax and data processing to plotting and programming, making them handy references to download for quick use.

Getting started

Introduction

This is a quick reference to getting started with Bash scripting.

  • Learn bash in y minutes(learnxinyminutes.com)
  • Bash Guide(mywiki.wooledge.org)

Example

Variables

String quotes

Shell execution

See Command substitution

Conditional execution

Functions

See: Functions

Conditionals

See: Conditionals

Strict mode

See: Unofficial bash strict mode

Brace expansion

ExpressionDescription
{A,B}Same as A B
{A,B}.jsSame as A.js B.js
{1..5}Same as 1 2 3 4 5

See: Brace expansion

Parameter expansions

Basics

See: Parameter expansion

Batch commands cheat sheet

Substitution

CodeDescription
${FOO%suffix}Remove suffix
${FOO#prefix}Remove prefix
${FOO%%suffix}Remove long suffix
${FOO##prefix}Remove long prefix
${FOO/from/to}Replace first match
${FOO//from/to}Replace all
${FOO/%from/to}Replace suffix
${FOO/#from/to}Replace prefix

Substrings

ExpressionDescription
${FOO:0:3}Substring (position, length)
${FOO:(-3):3}Substring from the right

Length

ExpressionDescription
${#FOO}Length of $FOO

Manipulation

Default values

ExpressionDescription
${FOO:-val}$FOO, or val if unset (or null)
${FOO:=val}Set $FOO to val if unset (or null)
${FOO:+val}val if $FOO is set (and not null)
${FOO:?message}Show error message and exit if $FOO is unset (or null)

Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset otherwise $FOO.

Loops

Basic for loop

C-like for loop

Ranges

With step size

Reading lines

Forever

Functions

Defining functions

Returning values

Raising errors

Arguments

ExpressionDescription
$#Number of arguments
$*All arguments
$@All arguments, starting from first
$1First argument
$_Last argument of the previous command

See Special parameters.

Conditionals

Conditions

Note that [[ is actually a command/program that returns either 0 (true) or 1 (false). Any program that obeys the same logic (like all base utils, such as grep(1) or ping(1)) can be used as condition, see examples.

ConditionDescription
[[ -z STRING ]]Empty string
[[ -n STRING ]]Not empty string
[[ STRING STRING ]]Equal
[[ STRING != STRING ]]Not Equal
[[ NUM -eq NUM ]]Equal
[[ NUM -ne NUM ]]Not equal
[[ NUM -lt NUM ]]Less than
[[ NUM -le NUM ]]Less than or equal
[[ NUM -gt NUM ]]Greater than
[[ NUM -ge NUM ]]Greater than or equal
[[ STRING =~ STRING ]]Regexp
(( NUM < NUM ))Numeric conditions

More conditions

ConditionDescription
[[ -o noclobber ]]If OPTIONNAME is enabled
[[ ! EXPR ]]Not
[[ X && Y ]]And
[[ X || Y ]]Or

Dos Commands Cheat Sheet

File conditions

ConditionDescription
[[ -e FILE ]]Exists
[[ -r FILE ]]Readable
[[ -h FILE ]]Symlink
[[ -d FILE ]]Directory
[[ -w FILE ]]Writable
[[ -s FILE ]]Size is > 0 bytes
[[ -f FILE ]]File
[[ -x FILE ]]Executable
[[ FILE1 -nt FILE2 ]]1 is more recent than 2
[[ FILE1 -ot FILE2 ]]2 is more recent than 1
[[ FILE1 -ef FILE2 ]]Same files

Example

Arrays

Defining arrays

Working with arrays

Operations

Iteration

Dictionaries

Defining

Declares sound as a Dictionary object (aka associative array).

Working with dictionaries

Iteration

Iterate over values

Iterate over keys

Options

Options

Glob options

Set GLOBIGNORE as a colon-separated list of patterns to be removed from globmatches.

History

Commands

CommandDescription
historyShow history
shopt -s histverifyDon’t execute expanded result immediately
Gam Command Cheat Sheet

Expansions

ExpressionDescription
!$Expand last parameter of most recent command
!*Expand all parameters of most recent command
!-nExpand nth most recent command
!nExpand nth command in history
!<command>Expand most recent invocation of command <command>

Operations

CodeDescription
!!Execute last command again
!!:s/<FROM>/<TO>/Replace first occurrence of <FROM> to <TO> in most recent command
!!:gs/<FROM>/<TO>/Replace all occurrences of <FROM> to <TO> in most recent command
!$:tExpand only basename from last parameter of most recent command
!$:hExpand only directory from last parameter of most recent command

!! and !$ can be replaced with any valid expansion.

Slices

CodeDescription
!!:nExpand only nth token from most recent command (command is 0; first argument is 1)
!^Expand first argument from most recent command
!$Expand last token from most recent command
!!:n-mExpand range of tokens from most recent command
!!:n-$Expand nth token to last from most recent command
Gam Command Cheat Sheet

!! can be replaced with any valid expansion i.e. !cat, !-2, !42, etc.

Miscellaneous

Numeric calculations

Subshells

Redirection

Inspecting commands

Trap errors

or

Case/switch

Source relative

printf

Batch Commands Cheat Sheet

Directory of script

Getting options

Heredoc

Reading input

Special variables

ExpressionDescription
$?Exit status of last task
$!PID of last background task
$$PID of shell
$0Filename of the shell script

See Special parameters.

Go to previous directory

Check for command’s result

Grep check

Also see

  • Bash-hackers wiki(bash-hackers.org)
  • Shell vars(bash-hackers.org)
  • Learn bash in y minutes(learnxinyminutes.com)
  • Bash Guide(mywiki.wooledge.org)
  • ShellCheck(shellcheck.net)