Bash


Streams

STDIN

The stream of characters input by the user by some input device, often represented as stream 0

STDOUT

The stream of characters output by the terminal, often represented as stream 1

STDERR

The stream of characters output by the terminal that occur due to a caught error, often represented as stream 2

Streams above 2 are legal however are custom

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

()

Initialises an array

Defines conditions of a block

{}

Used for defining functions

|

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

$(())

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 stout to file

>

Redirects stdout to file

<>

Redirects stream

>>

Appends stdout 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

*

Multiple character wildcard; represents zero or more characters

Represents variable array index in loops

@

Represents variable array index in loops

${}

Variable substitution operator

$()

Command substitution operator

!

Returns index of an object in an array in loops

!!

Represents previous command

$

Variable operator

""

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
}

functionName() {
 code
}

Streams

stdin - 0

stout - 1

stderr - 2

Programming commands

nasm

objdump

gdb

gcc

python

ruby

ld

Links

which

Displays binary path for applications

Assistance commands

man

Displays manual for certain command

man command

whatis

Displays explanation of a command

Cryptography commands

md5sum

Calculates MD5 hash for a file

cksum

Displays the checksum of a file

base64

Converts an input into base64

-d --decode

Decode base64 string into regular string

-i --ignore-garbage

ignore all non-alphanumeric characters when decoding

shred

Overwrites a file 35 times using the Gutmann method

Text processing commands

vim

Opens vim, a text editor

echo

Displays a string

-n

Do not print a trailing new line

-e

Enable interpretation of backslash codes

-E

Do not enable interpretation of backslash codes

cat

Concatenates a file (creates, views and edits files

grep

Searches for an expression in a text file

wc

Returns count of different character objects for a given input

diff

Display the difference between two ASCII files

head

Displays first 10 lines of a file

tail

Displays last 10 lines of a file

more

Displays an input as pages

less

jq

Formats JSON data

xxd

Displays a file interpreted as raw hexadecimal data

nano

Opens file in CLI text editor

sort

Sorts into alphabetical order

cut

Cuts away unwanted columns of a record

column*

Displays input with columns

awk

Processes input text based on a provided programming

sed

Stream Editor; replaces instances that match a pattern with a string

uniq

Returns pipelined file with no lines repeated

Network commands

wget

Retrieves files from the internet

traceroute

Displays possible packet routes for a given IP or domain

curl

Retrieves files from the internet

ifconfig

Interface Config; configures network interfaces

iwconfig

Wireless Interface Config; configures wireless network interface

nmcli

Manages network options and displays network information

msfconsole

Start metapsloit

getent

Resolves hostnames

dig*

Resolves DNS requesrs

ffuf

Fuzzing application written in go

git

Retrieve repositories from git.com

iwctl

Manages wireless connections

rtorrent

Controls the rtorrent application

ss*

Displays the status of each network port

ping

pings a node

netstat

Displays the status of each network port

netcat

Alos used as nc, opens netcat; tool for network reading and writing

mysql

Opens a MySQL connection

ssh

Connects to a server's shell over RSA encryption

rdesktop

Connects to an RDP server over RSA encryption

openvpn

Runs openvpn

nslookup

Name Server Lookup; Retrieves corresponding IP to a DNS record

exiftool

exec

Replace bash process with another process

rmdir

Removes a directory

scp

Securely copy a file over SSH encryption

nmap

pacman

Default package manager for Arch

yum

Manages packages from the yum repository

apt

Manages packages for Debian-based distributions

apachectl

rsync

Syncs directory to a network device

speedtest

Tests download and upload speeds by Ookla Speedtest

System management commands

ps

Displays processes of the current shell

htop*

Allows user to manage computer resources (resource monitor)

exit

Exits the terminal

lspci*

sudo

Executes command as root

sudo command

--list

-l, list sudo priveledges

uname

Displays OS infromation

neofetch

Displays system information

lscpu

Lists information about CPU

lsblk*

Lists block devices

lsusb

Lists information about USB devices

df

Disk Filesystem; displays information on filesystems

free

Displays information on memory

lsof

lspci

Lists PCI devices

kill

Kills a task

history

Displays user's command history

passwd

Modifies current user password

shutdown

Shuts computer down

systemctl

Allows users to manage processes

whoami

Displays which user is currently on the terminal

mount

Mounts a drive

umount

Unmounts a drive

crontab

Modifies cronjobs (files executed at regular intervals)

fdisk

Disk partiioning software

pactl

Controls the pulse audio application

parted

Disk partitioning software

timedatectl

halt

Shuts down the computer

reboot

Reboots the computer

id

uname

env*

init

Controls computer functionality such as graphics, terminal and power state

Filesystem commands

du

Returns the size of directory by recursively checking size of subdirectories

ls

Lists contents of current directory

find

Finds and displays files and directories recursively

locate

tree

Prints a tree of the filesystem

unalias

Allows user to stop replacing command with a word

pwd

Prints the working directory

cd

Changes the current directory

cp

Copies a file

rm

Removes a file

mv

Moves a file

mkdir

Makes a directory

touch

Allows user to access and modify file modification times

chown

chmod

Allows user to change file permissions through either normal or octal mode. In normal mode, a string describing the change of permissions is parsed. This string contains three parts; the users affected, the action, and the permissions in question.

User reference

Action reference

Permission reference

In octal mode, you parse a 3 octal digits that represent the permissions of ugo respectively, where the bits of each digit represent rwx.

chmod [OPTION]... OCTAL-MODE FILE...

unzip

Unzips file

find

Searches for a file in directory

ln

Makes a symlink (like a shortcut to another file on Windows)

file

Provides information on a file

file file.txt

stat

Provides file metadata

zip

Creates a zip file

dir

Prints directrory

7z

Extracts and creates archive files such as .zip and .7z files

startx

Runs xorg

hill

doas

cmatrix

Displays an awesome matrix

piper

alsamixer

node

mkfs

Makes a file system on a drive

mkswap

swapon

reflector

Set pacman mirrors

polybar

Controls the Polybar application

xdg-open

Opens xfce4 desktop

tr

tr [OPTIONS]... STRING1 [STRING2]

Translates occurences of a specific string in an input with another string

alias

Allows user to replace command with a word

sleep

Set a time delay for a program

test

Tests the terminal

let

Assigns a value to a variable

let "x = 1 + 2"

startxfce4

gnome-session

speed

Tests the speed of various hardware peripherals

Commenting

#

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