📓
Everything I Know
  • index
  • #
    • 3D Printing
  • A
    • Abandoned Spaces
    • ADHD
    • Aging
    • Algorithms & Data Structures
      • Array
      • Constraint Satisfaction Problem
      • Dynamic Programming
      • Graph
      • Hash Table
      • Heap
      • Linked List
      • Queue
      • Recursion
      • Set
      • Stack
      • Tree
      • Trie
      • Union Find
    • Amazon Web Services
    • Android
    • Anime, Comics & Manga
    • APIs
    • Artificial Intelligence
    • Assembly
      • ARM
      • MIPS
      • x86
    • Audio / Video Editing
    • Awesome
    • Azure
  • B
    • Board Games
    • Books
  • C
    • C (programming language)
    • C++
    • Cars
    • Cascading Style Sheets
    • Chess
    • Comedy
    • Command Line
      • Autotools
      • Awk
      • Bash scripting
      • Grep
      • Lsof
      • Sed
      • SSH
    • Competitive Programming
    • Compilers
    • Computer Graphics
      • OpenGL
      • Vulkan
      • WebGPU
    • Computer Networks
    • Computer Science
    • Concurrency
    • Continuous Integration / Delivery
    • Cooking
    • Cryptography
    • Cryptocurriencies
    • Curriculum Vitae
  • D
    • Databases
      • PostgreSQL
      • SQL
      • SQLite
    • Design Patterns
    • Digital Minimalism
    • Distributed Systems
    • Docker
    • Documentaries
    • Documentation
    • Domain Name System
    • Dopamine
    • Drawing
  • E
    • eCommerce
    • Electronics
      • Repairs
    • Engineering
    • Entrepreneurship
    • Events
  • F
    • Fashion
    • Fitness
      • Exercise
      • Nutrition
      • Weight Loss
    • Focus
    • Football
  • G
    • Game Development
      • Godot
      • LibGDX
      • Unity
      • Unreal Engine
    • Git
    • Goals
    • Guitar
  • H
    • Habits
    • Happiness
    • House
      • Tradespeople
      • Buying
      • Renting
  • I
    • Interviews
      • Behavioural Interviews
      • Coding Interviews
      • System Design Interviews
  • J
    • Java
    • JavaScript
      • Astro
      • Bun
      • Electron
      • Jest
      • Node.js
      • Nue.js
      • React.js
      • Redux
      • Vue.js
    • Journaling
  • K
    • Karting
    • Knots
    • Knowledge Bases
    • Kotlin
    • Kubernetes
  • L
    • LaTeX
    • Learning
      • Drawing
      • Languages
        • Certificate of Proficiency in English
        • Japanese
      • Piano
    • Legacy Code
    • LEGO
    • Lifestyle
    • Life Hacks
    • Linux
    • LISP
  • M
    • Machine Learning
      • Deep Learning
    • MacOS
    • Maths
    • Meditation
    • Movies
    • Music
      • Music Production
      • Music Theory
  • N
    • Negotiation
    • News
  • O
    • Operating Systems
      • Linux
  • P
    • Parenting
    • Personal Finance
      • ISAs
      • Pensions
    • PHP
    • Physics
    • Podcasts
    • Procrastination
    • Productivity
    • Programming
      • Functional Programming
      • Performance
    • Prometheus
    • Psychology
    • Public Speaking
    • Purpose
    • Puzzles
    • Python
      • Django
      • Pandas
  • Q
    • Quantum Computing
    • Quotes
  • R
    • Regular Expressions
    • Relationships
    • Reverse Engineering
    • Rust
      • Cargo
  • S
    • Security
      • Android
      • Binary Exploitation
      • CompTIA Security+ SYO-701
      • CTFs
      • Forensics
      • Linux
      • Web
      • Windows
    • Self Improvement
    • Shaving
    • Sitting
    • Sleep
    • Social Skills
    • Spring (framework)
    • Stoicism
    • Strength Training
      • Deadlifts
      • Push Ups
    • Success
    • System Design
      • Site Reliability Engineering
  • T
    • Table Tennis
    • Testing
    • Thinking
    • Touch Typing
    • Travel
      • Japan
        • Fukuoka
        • Hiroshima
        • Kyoto
        • Okinawa
        • Osaka
        • Tokyo
      • London
      • Rome
    • TV Series & Programmes
    • Twitch
    • TypeScript
    • Typography
  • V
    • Virtual Tours
    • Vim
    • Video Games
      • Emulation
      • Mods
      • Music
      • Speedrunning
      • Warzone
  • W
    • Web Apps
    • Web Cams
    • Web Development
      • Selenium
      • Web Assembly
    • Windows
      • Windows Development
    • Work
      • Freelancing
      • GitHub Profile
      • Interesting Companies
      • Job Boards
      • Remote Work
      • Startup
    • Writing
Powered by GitBook
On this page
  • Redirect to file
  • Comments
  • Conditional statements
  • Loops
  • Script input
  • Script output
  • Compare strings
  • Numbers and Arithmetic
  • Arrays
  • Functions
  • Files and directories
  • Debugging
  • Resources
  • Articles
  • Books
  • GitHub repositories
  • Websites

Was this helpful?

  1. C
  2. Command Line

Bash scripting

Redirect to file

# Write hello to file.txt (replacing the whole contents of the file)
echo "hello" > file.txt

# Append hello to the end of file.txt
echo "hello" >> file.txt

Comments

# this is a single line comment

: '
this
is a
multiline comment'

Conditional statements

# define a variable
count=10

# leave spaces before and after brackets
# reference variables with $
# eq, ne, lt, gt, le, ge
if [ $count -eq 10 ] 
then
    echo "first condition is true"
elif (( $count <= 9 ))
then
    echo "second condition is true"
else
    echo "all conditions are false"
fi # close if statement

age=10

# and statement - alternatives are
# if [[ "$age" -gt 18 && "$age" -lt 40 ]]
# if [ "$age" -gt 18 -a "$age" -lt 40 ]
if [ "$age" -gt 18 ] && [ "$age" -lt 40 ]
then
    echo "Age is correct"
else
    echo "Age is not correct"
fi

# or statement - alternatives are
# if [ "$age" -gt 18] || [ "$age" -lt 40 ]
# if [[ "$age" -gt 18 || "$age" -lt 40 ]]
if [ "$age" -gt 18 -o "$age" -lt 40 ]
then
    echo "Age is correct"
else
    echo "Age is not correct"
fi

# switch statement
car=$1 # read first argument
case $car in
    "BMW" )
        echo "It's a BMW" ;;
    "MERCEDES" )
        echo "It's a Mercedes" ;;
    "TOYOTA" )
        echo "It's a Toyota" ;;
    * ) # default case
        echo "Unknown car" ;;
esac # close switch statement

Loops

number=1

# while repeats when condition is true
# until repeats when condition is false
while [ $number -lt 10 ]
do
    echo "$number"
    # $(( ... )) allows for arithmetic
    number=$(( number+1 ))
done

# for larger ranges use {0..20}
# for different increments use {0..20..2}
for i in 1 2 3 4 5
do
    if [ $i -eq 5 ]
    then
        break
    fi
    echo $i
done

for (( i=0; i<5; i++ ))
do
    if [ $i -eq 3 ] || [ $i -eq 5 ]
    then
        continue
    fi
    echo $i
done

Script input

# print first, second, and third argument to the scrpt
# $0 is the script name
echo $1 $2 $3

# $@ = all the inputs
# (...) creates an arrary
args=("$@")
echo ${args[0]} ${args[1]} ${args[2]}
# print number of arguments
echo $#

# read line reads from input and saves into variable line
while read line
do
    echo "$line"
done < "${1:-/dev/stdin}" # read from first argument, /dev/stdin as default

Script output

# redirects stdout (1) to file.txt, and stderr (2) to file2.txt
ls -al 1>file.txt 2>file2.txt

# redirects 2 to whatever 1 is pointing to
ls -al > file.txt 2>&1

# redirects both stdout and stderr
ls -al >& file.txt

Compare strings

echo "enter first string"
read str1

echo "enter second string"
read str2

# string equality
# \< and \> are for string comparison 
if [ "$str1" == "$str2" ]
then
    echo "strings match"
else
    echo "strings don't match"
fi

Numbers and Arithmetic

# wrong! prints 31+21
echo 31+21

n1=4
n2=20
# print 24
echo $(( n1+n2 ))
echo $(expr $n1 + $n2)

Arrays

car=("BMW" "Toyota" "Honda")
# print all elements
echo "${car[@]}"
# print only the first element
echo "${car[0]}"
# print indices
echo "${!car[@]}"
# print lenght of the array
echo "${#car[@]}"
# delete Toyota from the array
unset car[1]
# set index 2 to Mercedes
car[2]="Mercedes"

Functions

function func()
{
    echo "this is a function"
}
# print this is a function
func

function funcPrint()
{
    echo $1
}
# print Hi
funcPrint Hi

function funcCheck()
{
    retValue="I love Linux"
}
retValue="I love MAC"
# print I love Mac
echo $retValue
funcCheck
# print I love Linux - funcCHeck overwrote retValue
echo $retValue

Files and directories

# -d checks if argument exists and is directory
if [ -d "MyFolder" ]
then
    echo "MyFolder exists"
fi

# -f checks if argument exists and is file
if [ -f "MyFile" ]
then
    echo "MyFile exists"
fi

Debugging

# execut script.sh in debug mode
bash -x ./script.sh

# debug only a section of the script
...
set -x
...
...
set +x
...

Resources

Articles

Books

GitHub repositories

Websites

PreviousAwkNextGrep

Last updated 11 months ago

Was this helpful?

- Shrikant Sharat Kandula

- Mendel Cooper

- The Programming Language compiled to Bash

- shell script analysis tool

- Google

Shell Script Best Practices
Advanced Bash-Scripting Guide
pure bash bible
pure sh bible
Amber
ShellCheck
Shell Style Guide