Bash Scripting Language Cheat Sheet Cheat Sheet: Danilobanjac
Bash Scripting Language Cheat Sheet Cheat Sheet: Danilobanjac
This is a draft cheat sheet. It is a work in progress and is not finished yet.
1 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
2 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
cat {testfile01,testfi‐ This will take as many files as we want and store red
black
le02} > test00 the content of those file in one file in this case "‐
white
test00". - as you can see for loop threated variable colors as a list. Because
red black white
echo "here is This will replace the whole content of the - if we put single quotes '$colors' this would take the actual word
something new" > wood.txt file with this new content, if we want to USING LET:
wood.txt add to file we need to use the command from - let "y=((x=20, 10/2))" (let in bash will let us to perform arithemic operation
- echo $y (This will return 5 because we separated the operation with comma)
below. CHANGING THE STRING TO UPPER OR LOWER:
- var = DSLConnection
echo "here is Double >> will add to file.
- echo ${var,} (This will change the first character of the string
comething new" >> - echo ${var,,} (This will change the whole string to lower)
wood.txt USING "\" ESCAPE CHARACTER:
- echo ""Linux is awesome""
: > wood.txt This will empty the whole file, remeber ":" that will output this Linux is awesome to the console.
says do nothing. - echo "\"Linux is awesome"\" (This will take quotes literaly)
will output this "Linux is awesome" to the console
rm wood.txt Will remove the file completely. REASIGN THE VALUE:
3 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
- let val=500/2
touch test1 test2 for example if we create a hundreds of file with
val2= echo $val (This will allow us to reasign the value from the first va
test3 test4 similar name how we could delete them all or echo "$val2"
select them all? (Solution below). will give same output "250"
IMPORTANT
rm test* ("*" astrix sign will mark everything that begins In order to get the value from any oder script example Python. Bash script wil
with test and delete it. print return_value()
USING ":" SIGN:
- var=20
Vim Commands if [ "$var" -gt 15 ]; then :;else echo "$var";fi (This ":" sign after then wi
IF STATEMENT USING "?" MARK:
Press "a" In order to start editing the file. - var=10
echo $(( var2=var1<20?1:0 )) (This will return the first value "
Press "esc" In order to quit editing the file.
CREATE ARRAY:
Write ":" To allow you to save or quit or write to file. - Colors=(red blue green white) (This will create an array in bash)
WRAP STRINGS INTO SOME CHARACTERS:
Write ":wg" To write those changes and quit with saving. - echo \+{test1,test2,test3}\+ (You can replace the "+" sign with any other sing
will output +test1+ +test2+ +test3+ to console
Write ":wg!" To force this action.
CREATE RANGE:
Write ":q!" To quit without saving. - echo {0..9} (This will print all numbers between 0 and 9 (including 9) same li
SEPARATE THE BLOCK OF CODE:
- var1=1
var2=2
{
var1=10
var2=12
}
echo "$var1 $var2" (The output to the console will be 10 and 12 because "
SAVE EXIT CODE FROM THE LAST COMMAND:
- python myPythonScript.py
ret=$?
if [ $ret -ne 0 ]; then
#Handle failure
#exit if required
fi
USE EXIT CODE TO MANIPULATE SCRIPT:
- #!/bin/bash
if [ $? -eq 0 ]
then
echo "Successfully created file"
exit 0
4 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
else
Part One (Variables and Parameters)
echo "Could not create file" >&2
exit 1
fi
HIDE WHOLE OUTPUT FROM THE SCRIPT:
- (
./manage.py create_test_database
) > /dev/null 2>&1
CONNECTING IF STATEMENTS:
- var=1
if [ "$var" -gt 0 ] && [ "$var" -eq 10 ]; then echo "THEN PART
var=1
if [ "$var" -gt 0 ] || [ "$var" -eq 10 ]; then echo "THEN PART
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)
5 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
ECHOING VARIABLES:
- var=10
echo var (This will echo "var")
echo $var (This will echo the actual value, this is why we are using dollar si
UNSET:
- var=10
unset var
echo $var ("unset" will actually reassign the value of "var" to null)
ASIGN VALUE TO THE VARIABLE THROUGH USER:
- echo "type in some value"
read var2 (This will ask user for input same like "prompt" in
echo $var2
PROPERTIES OF VARIABLES:
- var="T r a l a l a lalalal l"
echo $var (This will output the string but we will miss some spaces, this mean
var="T r a l a l a lalalal l"
echo "$var" (When we use "" to wrap our variable with as well the
DEFINE NULL VARIABLE:
- var= (This will define variable with a value of "null")
6 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
ENVIRONMENTAL VARIABLES:
- #!/bin/bash
if [ -n "$1" ]; then echo "1st one is $1"; fi ("$1" first argument that is be
if [ -n "$2" ]; then echo "2st one is $2"; fi
if [ -n "$3" ]; then echo "3rd one is $3"; fi
if [ -n "$4" ]; then echo "4th one is $4"; fi
if [ -n "$5" ]; then echo "5th one is $5"; fi
if [ -n "$6" ]; then echo "6th one is $6"; fi
if [ -n "$7" ]; then echo "7th one is $7"; fi
if [ -n "$8" ]; then echo "8th one is $8"; fi
if [ -n "$9" ]; then echo "9th one is $9"; fi
if [ -n "${10}" ]; then echo "10th one is ${10}"; fi
echo "List of arguments: "$"" ("$" this will take all the arguments that are be
echo "Name of Script: \""$0"\"" ("$0" is used to grab the name of the file)
7 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
RETURN VALUES:
- #!/bin/bash
NO_OF_ARGS=2
E_BADARGS=85
E_UNREADABLE=86
- Explanation: This script will accept two files and than compare
bash
8 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
PDF (recommended)
PDF (5 pages)
Alternative Downloads
5 Pages
9 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
© 2011 - 2023 Cheatography.com | CC License | Terms | Privacy Latest Cheat Sheets RSS Feed
10 of 11 12/26/23, 07:29
Bash Scripting Language Cheat Sheet Cheat Sheet by danilobanjac - Downloa... https://cheatography.com/danilobanjac/cheat-sheets/bash-scripting-langua...
11 of 11 12/26/23, 07:29