0% found this document useful (0 votes)
9 views

Bash Scripting Language Cheat Sheet Cheat Sheet by Danilobanjac - Download Free From Cheatography - Cheatography - Com - Cheat Sheets For Every Occasion

Bash Scripting Language Cheat Sheet Cheat Sheet

Uploaded by

ate29a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Bash Scripting Language Cheat Sheet Cheat Sheet by Danilobanjac - Download Free From Cheatography - Cheatography - Com - Cheat Sheets For Every Occasion

Bash Scripting Language Cheat Sheet Cheat Sheet

Uploaded by

ate29a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Bash Scripting Language Cheat Sheet Cheat Sheet by... https://cheatography.com/danilobanjac/cheat-sheets/b...

e.g., Regular Expressions, Garden, Kitchen, Audio

Home Cheat Sheets Create Community Help LOGIN or REGISTER

Download This Cheat Sheet (PDF) Comments Rating:

Home > Cheat Sheets > Bash Cheat Sheets

Bash Scripting Language Cheat Sheet Cheat Sheet (DRAFT) by


danilobanjac

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Commands Line Commands

clear Clears the screeen.

vim filename.sh Creates a .sh file.

sh filename.sh Execute Bash Script.

./filename.sh Other way of executing bash script.

ls -l See all file permissions inside a folder.

chmod +x filena- This will add execute permission to the file.


me.sh

ls --help This will open all available commands.

ls --help | grep "\-U" This will grab more information about "-U" command.

touch wood.txt This will create a file.

echo "here is This will write to the file that we first created.
something use it" >
wood.txt

cat wood.txt This will get us the output of the file.

cat {testfile01,testfi- This will take as many files as we want and store the
le02} > test00 content of those file in one file in this case "test00".

echo "here is This will replace the whole content of the wood.txt file
something new" > with this new content, if we want to add to file we need
wood.txt to use the command from below.

echo "here is Double >> will add to file.


comething new" >>
wood.txt

: > wood.txt This will empty the whole file, remeber ":" that says do
nothing.

rm wood.txt Will remove the file completely.

touch test1 test2 for example if we create a hundreds of file with similar
test3 test4 name how we could delete them all or select them all?
(Solution below).

rm test* ("*" astrix sign will mark everything that begins with
test and delete it.

Vim Commands

Press "a" In order to start editing the file.

Press "esc" In order to quit editing the file.

Write ":" To allow you to save or quit or write to file.

Write ":wg" To write those changes and quit with saving.

Write ":wg!" To force this action.

Write ":q!" To quit without saving.

1 of 3 2/12/24, 2:35
Bash Scripting Language Cheat Sheet Cheat Sheet by... https://cheatography.com/danilobanjac/cheat-sheets/b...

Part One (Variables and Parameters) Bash Script Programming Language Basics

ECHOING VARIABLES: ECHO:


- var=10 - echo "Some text" (This is same like "print" in python. It will print to command line)
echo var (This will echo "var") - echo "Some text" #This is comment ("This is comment" will not be executed)
echo $var (This will echo the actual value, this is DEFINING
why we are using dollar sign "$" before variable. So that program knows that we want a value from the variable)
VARIABLES:
UNSET: - name=10 (This desfines a variable with integer)
- var=10 - name=tea (This defines a variable)
unset var USING HASH "#":
echo $var ("unset" will actually reassign the value of "var"- to
echo
null)
"The word $name contains ${#name} chars" (This is how to do a string formatting in bash)
ASIGN VALUE TO THE VARIABLE THROUGH USER: - $(#name) (This will return a lenght of the string in bash)
- echo "type in some value" - $name will replace this part of the string with the current variable.
USING SEMICOLON:
read var2 (This will ask user for input same like "prompt" in javascript and store this value to variable)
echo $var2 - echo "hi there"; echo "you there?" (; semicolon sign will tell bash to run this as a next line of the code)
IF/THEN/ELSE STATEMENT:
PROPERTIES OF VARIABLES: - var=10
- var="T r a l a l a lalalal l" - if [ "$var" -gt 5 ]; then echo "YES"; else echo "NO"
echo $var (This will output the string but we will miss some spaces, thisnote
fi (Please means
thethat ot all
spaces of our
inside the spaces will
brackets be need
they printed that
to be is why
there for we are using
statement to "")
run, and every semicolon will be seen
var="T r a l a l a lalalal l" FOR LOOP:
- colors="red
echo "$var" (When we use "" to wrap our variable with as well black
the "$" dollar white"
sign then (This
all ofdefines a variable
our spaces will beasoutputed.
a string)This is recommended way of doing "echo" in bash)
DEFINE NULL VARIABLE: - for col in $colors
- var= (This will define variable with a value of "null") do (This will run the desired action)
echo "$col" (This will echo "col")
DECLARING VARIABEL ON SAME LINE: done (This will finish the for loop)
- var1=11 var2=22 var3=33 - if we run this script now the result below will be printed to console:
echo "$var1 $var2 $var3" (Same like in javascript) red
black
ASSIGNING, REASSIGNING AND UNSETTING THE VALUE: white
- var= - as you can see for loop threated variable colors as a list. Because "for col in $colors" dollar sign and colors will make a
echo "$var" red black white
var=9 - if we put single quotes '$colors' this would take the actual word $colors and print it.
echo "$var" USING LET:
var=10 - let "y=((x=20, 10/2))" (let in bash will let us to perform arithemic operations on variables)
echo "$var" - echo $y (This will return 5 because we separated the operation with comma)
unset var CHANGING THE STRING TO UPPER OR LOWER:
echo "$var" - var = DSLConnection
- echo ${var,} (This will change the first character of the string to lower)
ASSIGN VALUE FROM BASH COMMAND TO VARIABLE: - echo ${var,,} (This will change the whole string to lower)
- hi=$(ls -la) (This is the way of doing it) USING "\" ESCAPE CHARACTER:
echo "$hi" (Will print value of "hi") - echo ""Linux is awesome""
will output this Linux is awesome to the console.
- echo "\"Linux is awesome"\" (This will take quotes literaly)
ADDING TO A VARIABLE OR DOING ARITHMETIC OPERATIONS ON VARIABLES: will output this "Linux is awesome" to the console
- var= REASIGN THE VALUE:
let "var ++" - let val=500/2
echo "$var" (This will output 1) val2= echo $val (This will allow us to reasign the value from the first variabel to the second variabel)
let "var += 10" echo "$val2"
echo "$var" (This will output 10) will give same output "250"
IMPORTANT
REPLACING VALUES IN NUMBER: In order to get the value from any oder script example Python. Bash script will only recognise the value if value is printed
- num=1100 print return_value()
var=${num/10/B} USING ":" SIGN:
echo "$var" (This will echo "1B0") - var=20
if [ "$var" -gt 15 ]; then :;else echo "$var";fi (This ":" sign after then will actually tell our code to do nothing this w
ENVIRONMENTAL VARIABLES: IF STATEMENT USING "?" MARK:
- #!/bin/bash - var=10
echo $(( var2=var1<20?1:0 )) (This will return the first value "1" if statement is true and second value if statement is fa
MIN=10 (Minimum passed arguments in order to executeCREATE
the script)
ARRAY:
- Colors=(red blue green white) (This will create an array in bash)
if [ -n "$1" ]; then echo "1st one is $1"; fi ("$1"WRAP STRINGS
first INTOthat
argument SOMEisCHARACTERS:
being passed to script and "-n" check if argument exists. Returning either "true" or "false")
if [ -n "$2" ]; then echo "2st one is $2"; fi - echo \+{test1,test2,test3}\+ (You can replace the "+" sign with any other sing for example "$")
if [ -n "$3" ]; then echo "3rd one is $3"; fi will output +test1+ +test2+ +test3+ to console
if [ -n "$4" ]; then echo "4th one is $4"; fi CREATE RANGE:
if [ -n "$5" ]; then echo "5th one is $5"; fi - echo {0..9} (This will print all numbers between 0 and 9 (including 9) same like range() in python)
if [ -n "$6" ]; then echo "6th one is $6"; fi SEPARATE THE BLOCK OF CODE:
if [ -n "$7" ]; then echo "7th one is $7"; fi - var1=1
if [ -n "$8" ]; then echo "8th one is $8"; fi var2=2
if [ -n "$9" ]; then echo "9th one is $9"; fi {
if [ -n "${10}" ]; then echo "10th one is ${10}"; fi var1=10
var2=12
}
echo "List of arguments: "$"" ("$" this will take all the arguments that are being passed to script)
echo "Name of Script: \""$0"\"" ("$0" is used to grab the nameecho
of the
"$var1
file)
$var2" (The output to the console will be 10 and 12 because "{}" will separate this part of code)
SAVE EXIT CODE FROM THE LAST COMMAND:
if [ $# -lt "$MIN" ]; then echo "Not enought arguments, need -"$MIN"
pythontomyPythonScript.py
run!"; fi (This check "$#" number of all arguments being passed to script and compare it to our defined variable.)
- sh filename.sh 1 2 3 4 5 6 7 8 9 10 (Passing arguments to ourret=$?
script and printing them)
if [ $ret -ne 0 ]; then
#Handle failure
Part Two (Return Values)
#exit if required
fi
RETURN VALUES: USE EXIT CODE TO MANIPULATE SCRIPT:
- #!/bin/bash - #!/bin/bash

NO_OF_ARGS=2 touch /root/test 2> /dev/null


E_BADARGS=85
E_UNREADABLE=86 if [ $? -eq 0 ]
then
if [ $# -ne "$NO_OF_ARGS" ]; then echo "Usage: "$0" fileOne fileTwo"; exit $E_BADARGS;
echo "Successfully fi file"
created
if [ ! -r "$1" ] || [ ! -r "$2" ]; then echo "One or both filesexit
does0 not exist: "$1" or "$2""; exit "$E_UNREADABLE"; fi
( else
cmp $1 $2 echo "Could not create file" >&2
) > /dev/null 2>&1 exit 1
if [ $? -eq 0 ]; then echo "Files are the same!"; else echo "Files
fi are not the same!"; fi
exit 0 HIDE WHOLE OUTPUT FROM THE SCRIPT:
- (
- Explanation: This script will accept two files and than compare if ./manage.py
they are identical. We are defining our "EXIT CODES" and returning them depending on situation. (exit code 0 == good
create_test_database
) > /dev/null 2>&1

CONNECTING IF STATEMENTS:
- var=1
if [ "$var" -gt 0 ] && [ "$var" -eq 10 ]; then echo "THEN PART"; else echo "HELLOOO"; fi (Example with logical "and" statm
var=1
if [ "$var" -gt 0 ] || [ "$var" -eq 10 ]; then echo "THEN PART"; else echo "HELLOOO"; fi (Example with logical "or" statme
MODULO:
- let var=5%4
echo "$var" (Result will be one)
STRING UPPER CASE:
- some_word=tEsT
echo "${some_word^}" (This will grab first letter and make it upper case)
echo "${some_word^^}" (This will grab whole word and make it upper case)

bash

Download the Bash Scripting Language

2 of 3 2/12/24, 2:35
Bash Scripting Language Cheat Sheet Cheat Sheet by... https://cheatography.com/danilobanjac/cheat-sheets/b...

Cheat Sheet Cheat Sheet

PDF (recommended)

PDF (5 pages)

Alternative Downloads

PDF (black and white)


LaTeX
5 Pages

Latest Cheat Sheet Random Cheat Sheet

Roblox Lua Cheat Sheet Hausa Cheat Sheet


A simple refrence guide for any studio user who Basic Hausa Cheat Sheet
blanks on something
mojaam

NameLater2 29 Oct 11, updated 12 May 16


28 Nov 24 language, hausa, africa, nigeria
1 Page studio, lua, roblox 2 Pages English, (Hausa) ‫ﺱ‬
َ ‫( َﻫُﻮ‬Hausa)

(0) (1)

About Cheatography Behind the Scenes Recent Cheat Sheet Activity


Cheatography is a collection of 6597 cheat If you have any problems, or just want to say hi, NameLater2 published Roblox Lua.
sheets and quick references in 25 languages for you can find us right here: 3 days 21 hours ago
everything from linux to language! sandranieto updated Greek Learning.
DaveChild SpaceDuck
5 days 6 hours ago
Cheatography
mansourj updated Kubernetes - CKAD.
5 days 8 hours ago

© 2011 - 2024 Cheatography.com | CC License | Terms | Privacy Latest Cheat Sheets RSS Feed

3 of 3 2/12/24, 2:35

You might also like