BASH Cheat Sheet With StackOverflow References

Count lines in file

wc -l laravel.log
169613 laravel.log

Word count lines in Laravel.log

Truncate lines from the beginning of a file

tail -n +169000 laravel.log > laravel2.log
wc -l laravel2.log
614 laravel2.log

If statements

https://ryanstutorials.net/bash-scripting-tutorial/bash-if-statements.php

Example If Then Else Statement

#!/bin/bash
if [ $BACKUP_STATUS = 1 ] ; then
   result="success"
else
   result="failed"
fi

wget -q "https://backup-manager.test/api/job?api_token=xyz&id=1&status=$result"

How to Output the Date

https://www.shell-tips.com/linux/how-to-format-date-and-time-in-linux-macos-and-bash/

Calculate time differences

https://stackoverflow.com/questions/8903239/how-to-calculate-time-elapsed-in-bash-script

Source a file in the same directory

https://unix.stackexchange.com/questions/17717/refer-to-a-file-under-the-same-directory-of-a-script-found-in-path

Truthy / falsy (boolean) values

https://stackoverflow.com/questions/2953646/how-can-i-declare-and-use-boolean-variables-in-a-shell-script

Can you nest if statements in Bash?

4 Bash If Statement Examples ( If then fi, If then else fi, If elif else fi, Nested if )

Typical error:

line 44: [: =: unary operator expected

Bash Unary Operator Expected: What Does It Mean?

how to see in bash if a variable is set

https://www.cyberciti.biz/faq/see-check-if-bash-variable-defined-in-script-on-linux-unix-macos/

Output hostname in Bash

$HOSTNAME

Getting Hostname in Bash in Linux

How to concat variable in Bash

https://stackoverflow.com/questions/4181703/how-to-concatenate-string-variables-in-bash

A Bash function with variables

#!/bin/bash

greeting () {
   echo "Hello $1"
}

greeting "Joe"

That was fun!

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to Top