cheatsheet

Example

#!/bin/bash

NAME="Payam"
echo "Hello $NAME!"

exit 0

Variables:

varname=value                # defines a variable
varname=value command        # defines a variable to be in the environment of a particular subprocess
echo $varname                # checks a variable's value
read <varname>               # reads a string from the input and assigns it to a variable 
let <varname> = <equation>   # performs mathematical calculation using operators like +, -, *, /, %
export VARNAME=value         # defines an environment variable (will be available in subprocesses)
#Special shell variables
echo $$                      # prints process ID of the current shell
echo $!                      # prints process ID of the most recently invoked background job
echo $?                      # displays the exit status of the last command
echo $0                      # display Filename of the shell script

Quoting:

Redirection

Brace expansion

Parameter expansions

Basics

Substitution

Length

Default Values

Comment

Substrings

Manipulations

Conditionals:

Test Operators

In Bash, the test command takes one of the following syntax forms:

  • test EXPRESSION

  • [ EXPRESSION ]

  • [[ EXPRESSION ]]

To make the script portable, prefer using the old test [ command which is available on all POSIX shells. The new upgraded version of the test command [[ (double brackets) is supported on most modern systems using Bash, Zsh, and Ksh as a default shell. To negate the test expression, use the logical NOT (!) operator.

Checking Numbers

Note that a shell variable could contain a string that represents a number. If you want to check the numerical value use one of the following:

Checking Strings

Checking files

More conditions:

if statement:

Loops:

for:

while:

Until:

Case:

Functions:

Arrays

Dictionaries:

Debugging

Miscellaneous:

Payam Borosan.Goodluck

Last updated

Was this helpful?