Introduction and refresher

Introduction to Bash Scripting

Alex Scriven

Data Scientist

Introduction to the course

This course will cover:

  • Moving from command-line to a Bash script
  • Variables and data types in Bash scripting
  • Control statements
  • Functions and script automation
Introduction to Bash Scripting

Why Bash scripting? (Bash)

Firstly, let's consider why Bash?

  • Bash stands for 'Bourne Again Shell' (a pun)

  • Developed in the 80's but a very popular shell today. Default in many Unix systems, Macs

  • Unix is the internet! (Running ML Models, Data Pipelines)

    • AWS, Google, Microsoft all have CLI's to their products
Introduction to Bash Scripting

Why Bash scripting? (scripting!)

So why Bash scripting?

  • Ease of execution of shell commands (no need to copy-paste every time!)
  • Powerful programming constructs
Introduction to Bash Scripting

Expected knowledge

 

You are expected to have some basic knowledge for this course.

  • Understand what the command-line (terminal, shell) is
  • Have used basic commands such as cat, grep, sed etc.

If you are rusty, don't worry - we will revise this now!

Introduction to Bash Scripting

Shell commands refresher

Some important shell commands:

  • (e)grep filters input based on regex pattern matching
  • cat concatenates file contents line-by-line
  • tail \ head give only the last -n (a flag) lines
  • wc does a word or line count (with flags -w -l)
  • sed does pattern-matched string replacement
Introduction to Bash Scripting

A reminder of REGEX

'Regex' or regular expressions are a vital skill for Bash scripting.

You will often need to filter files, data within files, match arguments and a variety of other uses. It is worth revisiting this.

To test your regex you can use helpful sites like regex101.com

Introduction to Bash Scripting

Some shell practice

Let's revise some shell commands in an example.

Consider a text file fruits.txt with 3 lines of data:

banana
apple
carrot

If we ran grep 'a' fruits.txt we would return:

banana
apple
carrot
Introduction to Bash Scripting

Some shell practice

But if we ran grep 'p' fruits.txt we would return:

apple

Recall that square parentheses are a matching set such as [eyfv]. Using ^ makes this an inverse set (not these letters/numbers)

So we could run grep '[pc]' fruits.txt we would return:

apple
carrot
Introduction to Bash Scripting

Some shell practice

You have likely used 'pipes' before in terminal. If we had many many fruits in our file we could use sort | uniq -c

  • The first will sort alphabetically, the second will do a count
  • If we wanted the top n fruits we could then pipe to wc -l and use head
cat new_fruits.txt | sort | uniq -c | head -n 3
  14 apple
  13 bannana
  12 carrot
Introduction to Bash Scripting

Let's practice!

Introduction to Bash Scripting

Preparing Video For Download...