Skip to content

Instantly share code, notes, and snippets.

@miranda-zhang
Last active April 4, 2024 08:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miranda-zhang/9871f934edbb87a23c11185b85e191be to your computer and use it in GitHub Desktop.
Save miranda-zhang/9871f934edbb87a23c11185b85e191be to your computer and use it in GitHub Desktop.
bash shell script cheat sheet

Bash shell script cheatsheet

https://linuxconfig.org/bash-scripting-tutorial

find out where is your bash interpreter located

which bash

shebang: #!

Bash shell script starts with shebang:#! which is not read as a comment. First line is also a place where you put your interpreter which is in this case: /bin/bash.

example

#!/bin/bash

# enable extglobbing for selective removal of file
shopt -s extglob

cd /home/miranda/workspace/CodeBench/exercises/migrations

# rm all but __init__.py
rm !(__init__.py)

# clear mysql database
# read password from enviroment variable $MYSQL_DB_PASS
echo "DROP DATABASE IF EXISTS codebench;CREATE DATABASE codebench;" | mysql -uroot -p$MYSQL_DB_PASS

cd /home/miranda/workspace/CodeBench/
python manage.py makemigrations
python manage.py migrate
# python manage.py createsuperuser --username=admin --email=miranda.zhang.q@gmail.com
echo "from django.contrib.auth.models import User; User.objects.create_superuser('admin', 'miranda.zhang.q@gmail.com', 'admin')" | python manage.py shell

make the file executable

chmod +x *.sh

execute your bash script

./clear_data.sh

set environment variable

https://help.ubuntu.com/community/EnvironmentVariables

System-wide environment variables

sudo nano /etc/environment

You may add a line like:

DB_PASS="looooooooooooooooooooooogpass"

check if it has been setup correctly:

echo $TERM

If you are testing in a different (already opened) terminal, you need to restart it after the change.

Shell config files

Shell config files such as ~/.bashrc, ~/.bash_profile, and ~/.bash_login are often suggested for setting environment variables. While this may work on Bash shells for programs started from the shell, variables set in those files are not available by default to programs started from the graphical environment in a desktop session.

Multiline comments

: '
This is a
very neat comment
in bash
'

to explain: : is shorthand for true and true does not process any parameters. manual page: SYNOPSIS true [ignored command line arguments]

https://stackoverflow.com/questions/43158140/way-to-create-multiline-comments-in-bash#

echo

http://www.linfo.org/echo.html

No qoutes arount string. e.g. echo The number is $x.

Check if a program exists from a Bash script

$ command -v foo >/dev/null 2>&1 || { echo >&2 "I require foo but it's not installed.  Aborting."; exit 1; }

https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script

Array

## declare an array variable
declare -a arr=("element1" 
                "element2" "element3"
                "element4"
                )

## now loop through the above array
for i in "${arr[@]}"
do
   echo "$i"
   # or do whatever with individual element of the array
done

# You can access them using echo "${arr[0]}", "${arr[1]}" also

https://stackoverflow.com/questions/8880603/loop-through-an-array-of-strings-in-bash

https://www.thegeekstuff.com/2010/06/bash-array-tutorial/

Line number

PS4='Line ${LINENO}: ' bash -x install.sh https://stackoverflow.com/a/17805088/646732

Sed

Append new line after target

sed -i '/<anker line>/a <new line to append after anker line>' path/to/file

-i.bak save a back up file with extension .bak

sed -i 's/^psql_pswd=".*"/psql_pswd="'$psql_pswd'"/' $db_script

https://www.digitalocean.com/community/tutorials/the-basics-of-using-the-sed-stream-editor-to-manipulate-text-in-linux

Delete Word From File

echo 'This is a foo test' | sed -e 's/\<foo\>//g'
sed -e 's/\<regex-for-word\>//g' input > output

https://stackoverflow.com/questions/9633114/unix-script-to-remove-the-first-line-of-a-csv-file

sed 1q file.csv # print 1 line then quit
sed 1d file.csv > newfile # delete 1st line, save content to new file, original file not changed

Delete Line From File

To delete lines matching a pattern that contains forward slashes, a nonstandard character for a pattern delimiter can be used, the first use of that character must be escaped:

$ sed -i '\|^/dev/xvdb|d' /etc/fstab

https://stackoverflow.com/questions/25173267/sed-how-to-delete-lines-matching-a-pattern-that-contains-forward-slashes

Color

https://misc.flogisoft.com/bash/tip_colors_and_formatting

Awk

awk -F, '{print $2}' file.csv

Xtrace

# https://stackoverflow.com/a/5750463/646732
set -o xtrace
.
.
.
# set it back after script
set +o xtrace

Exit if any command fails

# exit the script if any command fails with exit 1
# https://stackoverflow.com/a/4346420/646732
set -e

Read

https://ss64.com/bash/read.html

while IFS="" read -r p || [ -n "$p" ]
do
  printf '%s\n' "$p"
done < $tempfile2

https://stackoverflow.com/a/1521498/646732

Uniq

https://www.thegeekstuff.com/2013/05/uniq-command-examples/

cat $input | sort | uniq > $output

Trim

echo "   foo bar  " | xargs

https://stackoverflow.com/questions/369758/how-to-trim-whitespace-from-a-bash-variable

Redirections

https://www.gnu.org/software/bash/manual/bashref.html#Redirections

Redirecting Input >

> file initialize file empty

Redirecting Output <

while IFS="" read -r line || [ -n "$line" ]
do

done < $tempfile2

feed file coment to some command

Appending Output >>

Here Documents <<

Here Strings <<<

Positional Parameters and Command Line Options

http://linuxcommand.org/lc3_wss0120.php

#!/bin/bash

echo "Positional Parameters"
echo '$0 = ' $0
echo '$1 = ' $1
echo '$2 = ' $2
echo '$3 = ' $3

Converting file to Unix format

dos2unix file.sh

https://stackoverflow.com/questions/11616835/r-command-not-found-bashrc-bash-profile/32912867

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment