Bash


Types

String

"This is a string!"

Array

("A" 2)

Arithmetic operators

+

Addition

1+2

Unary positive, makes number positive

+2

-

Subtraction

2-1

Unary negative, makes number negative

-2

*

Multiplication

2*3

/

Division

4/2

%

Modulo (remainder of division)

4%2

**

Indice

4**2

++

Increment

--

Decrement

Assignment operators

=

Equals (no space between variable and value)

x=2

Integer comparison operators

-eq

Is equal to

-ne

Is not equal to

-gt

Greater than

-lt

Less than

-ge

Greater than or equal to

-le

Less than or equal to

-z

Returns true if string is null

-n

Returns true if string is not null

String comparison operators

==

Is equal to

!=

Is not equal to

>

Greater than (ASCII order)

<

Less than (ASCII order)

>=

Greater than or equal to (ASCII order)

<=

Less than or equal to (ASCII order)

-z

Returns true if string is null

-n

Returns true if string is not null

Bitwise operators

&

Bitwise AND

|

Bitwise OR

^

Bitwise XOR

<<

Arithmetic shift left (shifts all the bits one space to the left, essentially multiplying it by 2)

>>

Logical shift right (shifts all the bits one space to the right, essentially dividing it by 2)

~

Bitwise NOT

Boolean operators

&&

AND operator

||

OR operator

!!

NOT operator

File test operators

-b

Checks if file is block special file

-c

Checks if file is character special file

-d

Checks if directory exists

-e

Checks if file exists

-r

Checks if file has read permission

-w

Checks if file has write permission

-x

Checks if file has execution permission

-s

Checks if file has size greater than 0

Miscellaneous operators

[]

References a array's index

Defines conditions of a block

Single character wildcard; for the characters contained within the brackets

[^]

Single character wildcard; for the characters not contained within the brackets

()

Initialises an array

Defines conditions of a block

{}

Used for defining functions

|

Pipeline; makes output of left command the input for the right

OR logic (grep extended)

$(())

Defines a mathematical expression

#!

Shebang; declares script as executable, followed by path to be executed in

#!/bin/bash

~

Represents home directory

/

Represents a directory

..

Represents parent directory

Represents numbers between two integers

.

Represents current directory

Single character wildcard

>

Redirects command output to file

<>

Redirects stream

>>

Appends an output to a file

&&

Execute second command if first command has no error

2>

Redirects command error output to file

1>&2

Redirects command output to command error output

2>&1

Redirects command error output to command output

?

Single character wildcard; represents a single character

Zero or single character wildcard (grep extended)

*

Multiple character wildcard; represents zero or more characters

Represents variable array index in loops

@

Represents variable array index in loops

${}

Array variable operator

!

Returns index of an object in an array in loops

!!

Represents previous command

$

Variable operator

End of line (grep)

+

Single or multiple character wildcard

^

Start of line (grep)

""

Declares a string

''

Declares a string

``

Declares a command output as a string

Argument specifiers

$#

Returns amount of parameters listed on execution

$@

Returns the parameters as one string

$?

Returns the return result of last command

$n

Returns the nth parameters

$*

Returns the parameters as one string

$0

Returns the file name of current script

$OSTYPE

Returns the OS type

$PATH

Returns the current path

$BASHPID

Returns the Process ID of the bash instance

$RANDOM

Returns a random unsigned 16 bit integer

Statements

if

Executes code on true condition

if [ condition ]
then
 code
fi

if ( condition )
then
 code
fi

case

Executes code on different cases

case variable in
 case1) code;;
 case2) code;;
 case3) code;;
esac

for

Executes code for every object in list

for foo in $list
do
 code
done

for foo in ~/directory; do
 code
done

while

Loops code on true condition

while [ condition ]
do
 code
done

while ( condition )
do
 code
done

until

Loops code on false condition

until [ condition ]
do
 code
done

until ( condition )
do
 code
done

function

Defines a function

function name {
 code
}

Param

Defines parameter array of a function

Param name

Default Commands

DowloadFile

Downloads a file from web-client

UploadFile

Uploads a file to web-client

DownloadString

Downloads a file from web-client instance as a string in memory

WriteAllBytes

Writes bytes to a file

DowloadFile

Downloads a file from web-client instance

ToBase64String

Converts a string into a Base64 string

Cmdlets

Invoke-Expression

IEX; executes a string as a cmdlet or command

Invoke-FileUpload

Fetches a file

Get-FileHash

Calculates the hash of a file according to some hashing algorithm

-Algorithm

Specifies an algorithm to use

Invoke-WebRequest

Sends a GET request and catches response

--all

--almost-all

--directory

--human-readable

--kibibytes

-l

-m

--recursive

--size

-S

-t

wc

Returns count of different character objects for a given input

--words

Returns the amount of words in an input

--chars

Returns the amount of characters in an input

--lines

Returns the amount of lines in an input

--bytes

Returns the amount of bytes in an input

Commenting

#

Used to tell compiler this line in the file is not to be compiled