The Only Bash Scripting Cheat Sheet That You Will Ever Need
The Only Bash Scripting Cheat Sheet That You Will Ever Need
The Only Bash Scripting Cheat Sheet That You Will Ever Need
DOJO
The Only Bash Scripting Cheat Sheet That You Will Ever Need
Bobby Iliev
Visit User Profile
Introduction
No matter if you are a DevOps/SysOps engineer, developer, or just a Linux enthusiast, you can
use Bash scripts to combine different Linux commands and automate boring and repetitive
daily tasks, so that you can focus on more productive and fun things.
Here you can find the completed Bash Scripting cheat sheet 👇
Bash Script Header (Shebang)
Option 1:
#!/bin/bash
Option 2:
#!/usr/bin/env bash
Variables
#!/bin/bash
name="DevDojo"
echo "Hi there $name"
User Input
#!/bin/bash
Comments
To do that in bash you need to add the # symbol at the beginning of the line. Comments will
never be rendered on the screen.
Arguments
#!/bin/bash
Arrays
# This would output the total number of elements in the array, in our case, it is 4:
echo "${#my_array[@]}"
File expressions
String expressions
# True if the shell variable varname is set (has been assigned a value).
[[ -v ${varname} ]]
# True if the strings are equal. = should be used with the test command for POSIX conforma
[[ ${string1} == ${string2} ]]
Arithmetic operators
# Returns true if the numbers are equal
[[ ${arg1} -eq ${arg2} ]]
# As with other programming languages you can use AND & OR conditions:
[[ test_case_1 ]] && [[ test_case_2 ]] # And
[[ test_case_1 ]] || [[ test_case_2 ]] # Or
Conditionals
#!/bin/bash
if [[ -z ${name} ]]
then
echo "Please enter your name!"
else
echo "Hi there ${name}"
fi
Loops
For loops
#!/bin/bash
While loops
#!/bin/bash
counter=1
while [[ $counter -le 10 ]]
do
echo $counter
((counter++))
done
Until Loops
#!/bin/bash
count=1
until [ $count -gt 10 ]
do
echo $count
((count++))
done
Functions
function function_name() {
your_commands
}
Example
#!/bin/bash
function hello(){
echo "Hello World Function!"
}
hello
Conclusion
For a more in-depth explanations check out this online guide here:
DigitalOcean offers a simple and reliable cloud hosting solution that enables developers to get their website or
application up and running quickly.
The official Laravel job board. Find the best and most talented Laravel developers by posting your job on the official
Laravel job board.
View Website
Learn how to code your own blockchain and create your own crypto-currency with the CoinCamp interactive and fun
online training platform.
If you want to learn more about Bash Scripting check out this free eBook here:
If you prefer video content, you could take a look at this mini video crash course here:
Comments (0)