Created
December 4, 2018 08:13
-
-
Save gillyb/562826b97fb087c1dd483bb09748b2e8 to your computer and use it in GitHub Desktop.
A simple bash cheat sheet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
### | |
### Some terminal colors | |
### usage: echo -e "${GREEN}Hello, this should be green! $RESET" | |
### | |
export BLACK="\033[30m" | |
export GRAY="\033[1;30m" | |
export RED="\033[31m" | |
export GREEN="\033[32m" | |
export YELLOW="\033[33m" | |
export BLUE="\033[34m" | |
export CYAN="\033[36m" | |
export UNDERLINE="\033[4m" | |
export RESET="\033[0m" | |
export CLEAR_LINE="\r\033[K" | |
### Getting the current directory into a variable | |
CURRENT_DIR=$(pwd) | |
### Returning to the original directory we saved | |
cd $CURRENT_DIR | |
### | |
### Iterating over an array of strings | |
### | |
MY_STRINGS=("first" "second" "third") | |
for s in "${MY_STRINGS[@]}"; do | |
echo "$s" | |
done | |
### | |
### Functions | |
### | |
some_function() { | |
echo "This does something interesting..." | |
echo "The first parameter sent to this function was $1" | |
echo "...and the second parameter was $2" | |
} | |
### Calling the function with parameters | |
some_function "first_param" "second_param" | |
### | |
### Conditions | |
### | |
### equality | |
if [ $SOME_PARAM == "hello" ]; then # The spaces around the '==' are necessary! | |
echo "$SOME_PARAM is 'hello' " | |
fi | |
# or this way | |
if [ $SOME_PARAM -eq "hello" ]; then | |
fi | |
### not equal | |
if [ $SOME_PARAM -ne "hello" ]; then | |
echo "not equal" | |
fi | |
### string null or empty | |
if [ -z $SOME_PARAM ]; then | |
echo "$SOME_PARAM is null or empty" | |
fi | |
### logical 'or' and logical 'and' operators respectively | |
if [ $SOME_PARAM -o $ANOTHER_PARAM ]; then | |
echo "one of them was true" | |
fi | |
if [ $SOME_PARAM -a $ANOTHER_PARAM ]; then | |
echo "both of them were true | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment