Bash Scripting
SigmaPhi (11)

Important Notice

  • This tutorial is a work in progress, expect more topics to be covered later
  • Everything in this tutorial works in, unless stated otherwise
  • This is important to mention because it can overcome a suprising number of's limitiations
  • This tutorial assumes that you have basic knowledge of programming, specifically variables and functions (also known as procedures, methods)

What is Bash

Bash (Bourne Again SHell) is a open-source shell for the GNU project (replacing the Bourne Shell). It is the default shell for most Linux distros, macOS aswell, and in Windows 10 via WSL.

The bash language is a shell scripting language, used for configuration, package management, automation of routine tasks and more. It is similar in purpose to powershell and (somewhat less so with) applescript.
Everything covered in this tutorial can be utilized in the implemenation of Bash.

The (non comprehensive) list of known limitations (as of April 2019):

  • unavailabilty sudo (for security reasons)
  • aplay command (audio programming is not possible)

Basics of Bash

Hello World

The function echo and printf can be used to print to the terminal

> echo "hello world!"                #echo automatically appends a newline
hello world                          #'-n' flag is used to remove the newline (echo -n "str")

> printf "hello world!\n"
hello world

I recommend printf if you need to format tabs or newline as it is more consistent across different terminals

Everything is a string

In bash everything is a string and what they do is dependent on context.
Bash uses spaces as delimiters (seperators) so, hello world is converted to the strings 'hello' and 'world'. Use single or double quotes to make it a single string, ie: "hello world" or 'hello world'.
The first string in a line is used as the command

> echo hello world                    #The strings are 'echo', 'hello', 'world'
                                      #The first string echo is the command
                                      #'hello' and 'world' are the first and second arguments


echo hello, echo "hello", "echo" "hello" are equivalent, but not "echo hello" as the former are two strings and the latter is one string


It is important to be careful with spaces during assignment

> Name=Amjad                          #assign "Amjad" to Name
> Name =Amjad                         #call Name with argument "=Amjad"
> Name = Amjad                        #call Name with arguments '=' and "Amjad"

The $ in $Hello tells Bash to replace the string Hello with the variable Hello.

> Name=Amjad                          #Mutable (non-constant) variables
> echo $Name                          #The '$' is used

Reassignment & Constants

> Name="Haya"                         #Reassignment
> echo "$Name"                        #"$NAME" is equal to $NAME

> echo '"$NAME"'                      #'"$NAME"' is not equal to $NAME

readonly Change="Constant"            #Constant variables
> Change="Changing"                   #Causes error as variable is constant

Command Flags

Flags are used to toggle specific options
Single hypens '-' are used for multiple single letter flags

> rm -rf "dir"
  rm                                  #remove command
     -r                               #option: remove folders recursively
       f                              #option: remove files
         "dir"                        #directory to delete

Double hypens '-' are used for one multi-letter flag

> git --help


Special Parameters

$1                                    #First parameter
$2                                    #Second parameter
$n                                    #Nth parameter

$#                                    #Number of parameters
$?                                    #Return value of most recent function
#$* and [email protected] are identical if not quoted
#Otherwise they are different

"$*" = "$1 $2 $3 $4 ..."
"[email protected]" = ""$1""$2""$3"..."

Function Declaration

Functions are declared as follows

fn_name(){                            #arguments are not placed here
                                      #function body

fn_name args                          #function call

Bash as a Control Language

Bash is a fundamental domain specific language in linux, and serves as it's control interface, like Apple's applescript and Window's powershell
It can be used to automate many routine tasks such as file management

Linux File Commands

The basic file system of Linux is structured as follows (Only relevant files are shown)
The starting directory is called the root directory '/', which is approximately equivalent to C:\ on windows


File Command are

cd                                   #change working (current) directory
ls                                   #list segments (folders)
mkdir                                #make (new) directory (folder)
rm                                   #remove file or folder

Compiling with GCC

@Scoder12 had provided the general idea of using Bash to compile programs
GCC, Make and Git and installed in


Makefiles can be used to reduce compilation time by only compiling the changed files


.PHONY: build_c
    gcc -o output source.c

.PHONY: build_cpp
    g++ -o output source.cpp
> make build_c
> make build_cpp

Linking Libraries

> g++ -o main main.cpp -lNAME_OF_LIBRARY
> g++ -o main main.cpp -lncurses     #to link ncurses   

Default Linux libraries (ncurses.h, unistd.h) are already included in
Any unavailable libraries (SDL2, SFML, ...) can be downloaded from their respective sites and then uploaded to to be used in your project or cloned and compiled via git


GCC can compile C or C++ to assembly

//C source

int main(){
  return 0;
> gcc -S -o assembly.s source.c      #Compile to assembly
; assembly.s
; Assembly output

	.file	"main.c"
	.globl	main
	.type	main, @function
	movl	$0, %eax
	.size	main, .-main
	.ident	"GCC: (GNU) 8.1.0"
	.section	.note.GNU-stack,"",@progbits
> gcc -c -o program.o assembly.s     #Assemble
> gcc -o executable program.o        #Linking
> ./executable                       #Execution


It can also be used to access git

> git --help
> git clone "git-repo-url"

Here is a good post on using git by @eankeen

You are viewing a single comment. View All
SigmaPhi (11)

This Tutorial is a work in progress