Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Basic Shell Scripting
#! /bin/bash
# ECHO COMMAND
# echo Hello World!
# VARIABLES
# Uppercase by convention
# Letters, numbers, underscores
NAME="Bob"
# echo "My name is $NAME"
# echo "My name is ${NAME}"
# USER INPUT
# read -p "Enter your name: " NAME
# echo "Hello $NAME, nice to meet you!"
# SIMPLE IF STATEMENT
# if [ "$NAME" == "Brad" ]
# then
# echo "Your name is Brad"
# fi
# IF-ELSE
# if [ "$NAME" == "Brad" ]
# then
# echo "Your name is Brad"
# else
# echo "Your name is NOT Brad"
# fi
# ELSE-IF (elif)
# if [ "$NAME" == "Brad" ]
# then
# echo "Your name is Brad"
# elif [ "$NAME" == "Jack" ]
# then
# echo "Your name is Jack"
# else
# echo "Your name is NOT Brad or Jack"
# fi
# COMPARISON
# NUM1=31
# NUM2=5
# if [ "$NUM1" -gt "$NUM2" ]
# then
# echo "$NUM1 is greater than $NUM2"
# else
# echo "$NUM1 is less than $NUM2"
# fi
########
# val1 -eq val2 Returns true if the values are equal
# val1 -ne val2 Returns true if the values are not equal
# val1 -gt val2 Returns true if val1 is greater than val2
# val1 -ge val2 Returns true if val1 is greater than or equal to val2
# val1 -lt val2 Returns true if val1 is less than val2
# val1 -le val2 Returns true if val1 is less than or equal to val2
########
# FILE CONDITIONS
# FILE="test.txt"
# if [ -e "$FILE" ]
# then
# echo "$FILE exists"
# else
# echo "$FILE does NOT exist"
# fi
########
# -d file True if the file is a directory
# -e file True if the file exists (note that this is not particularly portable, thus -f is generally used)
# -f file True if the provided string is a file
# -g file True if the group id is set on a file
# -r file True if the file is readable
# -s file True if the file has a non-zero size
# -u True if the user id is set on a file
# -w True if the file is writable
# -x True if the file is an executable
########
#CASE STATEMENT
# read -p "Are you 21 or over? Y/N " ANSWER
# case "$ANSWER" in
# [yY] | [yY][eE][sS])
# echo "You can have a beer :)"
# ;;
# [nN] | [nN][oO])
# echo "Sorry, no drinking"
# ;;
# *)
# echo "Please enter y/yes or n/no"
# ;;
# esac
# SIMPLE FOR LOOP
# NAMES="Brad Kevin Alice Mark"
# for NAME in $NAMES
# do
# echo "Hello $NAME"
# done
# FOR LOOP TO RENAME FILES
# FILES=$(ls *.txt)
# NEW="new"
# for FILE in $FILES
# do
# echo "Renaming $FILE to new-$FILE"
# mv $FILE $NEW-$FILE
# done
# WHILE LOOP - READ THROUGH A FILE LINE BY LINE
# LINE=1
# while read -r CURRENT_LINE
# do
# echo "$LINE: $CURRENT_LINE"
# ((LINE++))
# done < "./new-1.txt"
# FUNCTION
# function sayHello() {
# echo "Hello World"
# }
# sayHello
# FUNCTION WITH PARAMS
# function greet() {
# echo "Hello, I am $1 and I am $2"
# }
# greet "Brad" "36"
# CREATE FOLDER AND WRITE TO A FILE
# mkdir hello
# touch "hello/world.txt"
# echo "Hello World" >> "hello/world.txt"
# echo "Created hello/world.txt"
@Adeelasd

This comment has been minimized.

Copy link

@Adeelasd Adeelasd commented May 20, 2018

ty

@ddpetty

This comment has been minimized.

Copy link

@ddpetty ddpetty commented May 20, 2018

Thank you for this tutorial!

@SwaroopMeher

This comment has been minimized.

Copy link

@SwaroopMeher SwaroopMeher commented May 23, 2018

Thank you so much!

@elStranger

This comment has been minimized.

Copy link

@elStranger elStranger commented Jul 22, 2018

thx BRAD!

@GuyDev1

This comment has been minimized.

Copy link

@GuyDev1 GuyDev1 commented Jul 22, 2018

Thanks!

@albersaber

This comment has been minimized.

Copy link

@albersaber albersaber commented Oct 10, 2018

nice and well organized tutorial , thanks a lot

@zorguiradhwen

This comment has been minimized.

Copy link

@zorguiradhwen zorguiradhwen commented Oct 23, 2018

thanks man

@adityakulai07

This comment has been minimized.

Copy link

@adityakulai07 adityakulai07 commented Oct 25, 2018

Thanks!

@arekgorecki

This comment has been minimized.

Copy link

@arekgorecki arekgorecki commented Nov 17, 2018

Thanks a lot

@IrakliDevelop

This comment has been minimized.

Copy link

@IrakliDevelop IrakliDevelop commented Jan 16, 2019

Thank you for the tutorial

@eichgi

This comment has been minimized.

Copy link

@eichgi eichgi commented Feb 14, 2019

Thanks.

@arshaknorsoyan

This comment has been minimized.

Copy link

@arshaknorsoyan arshaknorsoyan commented Feb 26, 2019

Thanks for the tutorial!

@vishnuraghavb

This comment has been minimized.

Copy link

@vishnuraghavb vishnuraghavb commented Mar 13, 2019

Your tutorial was awesome! Thanks for the cheat sheet 👍

@suarezluis

This comment has been minimized.

Copy link

@suarezluis suarezluis commented Apr 21, 2019

For some reason in ubuntu I cant declare a function like;
function hello () {
echo "Hello"
}

but instead;
hello () {
echo "Hello"
}

@asimaarbi

This comment has been minimized.

Copy link

@asimaarbi asimaarbi commented May 1, 2019

Awesome!

@ParrotStone

This comment has been minimized.

Copy link

@ParrotStone ParrotStone commented May 4, 2019

👍

@ibrahim0814

This comment has been minimized.

Copy link

@ibrahim0814 ibrahim0814 commented Jul 1, 2019

Good stuff, love this

@jetobe95

This comment has been minimized.

Copy link

@jetobe95 jetobe95 commented Aug 2, 2019

Thanks for this video

@tundereuben

This comment has been minimized.

Copy link

@tundereuben tundereuben commented Oct 21, 2019

Thanks Brad. This is very helpful.

@jawwwr

This comment has been minimized.

Copy link

@jawwwr jawwwr commented Oct 22, 2019

Just in time for my DevOps tasks, thank you!

@jayDevaney

This comment has been minimized.

Copy link

@jayDevaney jayDevaney commented Dec 9, 2019

Thanks Brad!

@Chiango2

This comment has been minimized.

Copy link

@Chiango2 Chiango2 commented Jan 20, 2020

Thanks a million Brad

@toxic36282

This comment has been minimized.

Copy link

@toxic36282 toxic36282 commented Feb 8, 2020

amazing brother thanks for sharing

@selvesandev

This comment has been minimized.

Copy link

@selvesandev selvesandev commented Feb 8, 2020

awesome !!! Thank you

@TestardR

This comment has been minimized.

Copy link

@TestardR TestardR commented Mar 21, 2020

Nice

@toxic36282

This comment has been minimized.

Copy link

@toxic36282 toxic36282 commented Mar 21, 2020

You are always great.. differ way and simple. love that

@Mrudul-Bhatt

This comment has been minimized.

Copy link

@Mrudul-Bhatt Mrudul-Bhatt commented Mar 22, 2020

You are awesome :D

@PowerUser64

This comment has been minimized.

Copy link

@PowerUser64 PowerUser64 commented Mar 27, 2020

Thanks, man.

@ismail-fathi

This comment has been minimized.

Copy link

@ismail-fathi ismail-fathi commented Jun 30, 2020

Thanks a lot Brad! ❤️

@nahush2321

This comment has been minimized.

Copy link

@nahush2321 nahush2321 commented Aug 5, 2020

Thank you

@nikhildosapati6

This comment has been minimized.

Copy link

@nikhildosapati6 nikhildosapati6 commented Aug 28, 2020

Thanks brad! <3

@ANDRUXUIS

This comment has been minimized.

Copy link

@ANDRUXUIS ANDRUXUIS commented Aug 31, 2020

Very Helpful

@milanu13

This comment has been minimized.

Copy link

@milanu13 milanu13 commented Sep 2, 2020

This is awesome! Thanks Brad.

@jacinyan

This comment has been minimized.

Copy link

@jacinyan jacinyan commented Sep 10, 2020

Much appreciated

@blaq-swan

This comment has been minimized.

Copy link

@blaq-swan blaq-swan commented Sep 11, 2020

This is great, Brad! Thank you.

@Raj2503

This comment has been minimized.

Copy link

@Raj2503 Raj2503 commented Sep 18, 2020

Thanks a lot, Brad!

@xryder101

This comment has been minimized.

Copy link

@xryder101 xryder101 commented Sep 26, 2020

Thank you so much Brad!!

@xryder101

This comment has been minimized.

Copy link

@xryder101 xryder101 commented Sep 26, 2020

All of your videos are absolutely amazing!!!!!!!!!!!!!!!!!!

@jerrysonguillen

This comment has been minimized.

Copy link

@jerrysonguillen jerrysonguillen commented Oct 1, 2020

Thanks a lot, Brad!

@Anupama-Dikkumbura

This comment has been minimized.

Copy link

@Anupama-Dikkumbura Anupama-Dikkumbura commented Oct 7, 2020

Thank you :)

@wbai-aptiv

This comment has been minimized.

Copy link

@wbai-aptiv wbai-aptiv commented Oct 14, 2020

thanks

@nijogeorgep

This comment has been minimized.

Copy link

@nijogeorgep nijogeorgep commented Oct 15, 2020

Thank You 👍

@dirtflake

This comment has been minimized.

Copy link

@dirtflake dirtflake commented Oct 23, 2020

cool!

@bhavik-kanejiya

This comment has been minimized.

Copy link

@bhavik-kanejiya bhavik-kanejiya commented Oct 25, 2020

Superb!

@constancemaurer

This comment has been minimized.

Copy link

@constancemaurer constancemaurer commented Nov 1, 2020

<3

@vinithabm

This comment has been minimized.

Copy link

@vinithabm vinithabm commented Nov 6, 2020

Thank you 👍

@obaranovskyi

This comment has been minimized.

Copy link

@obaranovskyi obaranovskyi commented Nov 15, 2020

Thank you!

@LGOG

This comment has been minimized.

Copy link

@LGOG LGOG commented Nov 15, 2020

Thank you!

@Ali-Alawieh

This comment has been minimized.

Copy link

@Ali-Alawieh Ali-Alawieh commented Nov 22, 2020

<3

@shivampip

This comment has been minimized.

Copy link

@shivampip shivampip commented Dec 4, 2020

Thank you, Brad
here is the same gist uncommented for better syntax highlighting.

@Areskyb

This comment has been minimized.

Copy link

@Areskyb Areskyb commented Dec 8, 2020

Thank you Brad!!

@Daxtor134

This comment has been minimized.

Copy link

@Daxtor134 Daxtor134 commented Dec 24, 2020

Thank you, this helps a lot!

@ranarashid426

This comment has been minimized.

Copy link

@ranarashid426 ranarashid426 commented Dec 27, 2020

Thanks man ,You are awesome

@ganesantechi

This comment has been minimized.

Copy link

@ganesantechi ganesantechi commented Jan 15, 2021

Thanks for sharing this comments..

@thomasgrusz

This comment has been minimized.

Copy link

@thomasgrusz thomasgrusz commented Jan 23, 2021

Helpful, thanks!

@lorezi

This comment has been minimized.

Copy link

@lorezi lorezi commented Feb 1, 2021

Thanks Brad

@coop59

This comment has been minimized.

Copy link

@coop59 coop59 commented Feb 20, 2021

Very nicely done, I got some of the basics out of the way. Thank you sir...... 👍

@fddemora

This comment has been minimized.

Copy link

@fddemora fddemora commented Mar 1, 2021

Great!

@farhan418

This comment has been minimized.

Copy link

@farhan418 farhan418 commented Mar 4, 2021

Thank you

@Itsnexn

This comment has been minimized.

Copy link

@Itsnexn Itsnexn commented Mar 25, 2021

good one !!!

@amushate

This comment has been minimized.

Copy link

@amushate amushate commented Apr 7, 2021

Thanks chief

@amjack59

This comment has been minimized.

Copy link

@amjack59 amjack59 commented Apr 24, 2021

Thanks for your help and great videos! Much appreciated and well done.

@DEALTALFA

This comment has been minimized.

Copy link

@DEALTALFA DEALTALFA commented May 22, 2021

short but crisp content thank you very much

@Abhishek12345679

This comment has been minimized.

Copy link

@Abhishek12345679 Abhishek12345679 commented May 28, 2021

been three years <3 since he uploaded the video and the video is teaching people :3

@Kamaib24

This comment has been minimized.

Copy link

@Kamaib24 Kamaib24 commented Jun 7, 2021

Thank you

@alimash76

This comment has been minimized.

Copy link

@alimash76 alimash76 commented Jun 11, 2021

Thank you

@cleverfaizan786

This comment has been minimized.

Copy link

@cleverfaizan786 cleverfaizan786 commented Jul 14, 2021

Thanks man

@Barrcap

This comment has been minimized.

Copy link

@Barrcap Barrcap commented Aug 1, 2021

Thanks!

@xoxwgys56

This comment has been minimized.

Copy link

@xoxwgys56 xoxwgys56 commented Aug 5, 2021

Awesome!

@kingpenguin001

This comment has been minimized.

Copy link

@kingpenguin001 kingpenguin001 commented Aug 26, 2021

and im getting started!
Thanks Brad!

@Hellyen211

This comment has been minimized.

Copy link

@Hellyen211 Hellyen211 commented Aug 31, 2021

Well done! Thanks!

@ShahJugalR

This comment has been minimized.

Copy link

@ShahJugalR ShahJugalR commented Sep 2, 2021

Thanks Chicha

@abhinavkeshari

This comment has been minimized.

Copy link

@abhinavkeshari abhinavkeshari commented Oct 18, 2021

👍🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment