Predictive Hacks

Tutorial in Bash (Unix Shell) Scripting

Invest in you. Online courses from $11.99

Bash is a concise, superfast, and robust scripting language for data and file manipulation. It’s a vital skill for building analytics pipelines in the cloud, favored by Linux users to work with data stored across multiple files. In this post, we provide practical examples of how you can create your own Bash functions to automate your job as a Data Scientist.

This tutorial is based totally on the DataCamp Course which I found really helpful and important for every Data Scientist.

For this tutorial, it may be helpful to have a look at some basic Unix Commands, at some Basic Unix Examples as well as at a Cron Job Example.

“Hello World” Bash Script Example

Usually, a bash script starts with a shebang #!/usr/bash which indicates to execute the file using the Bash Shell. Let’s say that we want to run a Bash Script that prints “Hello World” and “Good Night World”.

echo "Hello World"
echo "Good Night World"

The script file can be saved as where the file extension .sh indicates that this is a Bash Script. Technically not needed if the first line has the shebang and path to Bash. It can be run in the terming using the bash or if you have mentioned the first line the !/usr/bash you can simply type ./ . Getting back to our example. In the terminal, we type:


and we get:

Hello World
Good Night World

Let’s give another example, using the pipe commands. Assume that we have a file called employees.csv with the UserID of the Employees and their Department. Like (top 5 lines):


and we want to create a Bash Script that returns the number of employees by department. Let’s call our script and will be as follows:

cat employees.csv | cut -d "," -f 2| tail -n +2 | sort | uniq -c | sort -nr
10 Data_Science
5 Project_Management
3 Human_Resources


In Bash scripting, there are 3 ‘streams’

  • STDIN (standard input). A stream of data into the program.
  • STOUT (standard output). A stream of data out of the program. It has an exit code value of 0 for success and 1 for failure.
  • STDERR (standard error). Errors in your program. It has a value of 2.

How to use exit codes in scripts


cat file.txt 

if [ $? -eq 0 ]
  echo "The script ran ok"
  exit 0
  echo "The script failed" >&2
  exit 1


Bash scripts take arguments specified when making the execution call fo the script. ARGV is a term to describe all the arguments that are fed into the script. Each argument can be accessed via the $ notation. The first as $1, the second as $2 and so on. Finally. the [email protected] and $* give all the arguments in ARGV and the $# gives the length (i.e. the number) of arguments.

Example. Let’s consider the

# Echo the first and second ARGV arguments
echo $1
echo $2

# Echo out the entire ARGV array
echo [email protected]

# Echo out the size of ARGV
echo "There are " $# " arguments"

And let’s run:

bash one two three four five

We get:

one two three four five
There are 5 arguments

Basic Variables in Bash

Similar to other languages, you can assign variables with the equals notation like:

# do not use space between equal sign
# you can reference them with the $
echo "Hello" $firstname $lastname
Hello George Pipis

In Bash, using different quotation marks can mean different things. Both when creating variables and printing.

  • Single quotes: Shell interprets what is between literally
  • Double quotes: Shell interprets literally except using $ and bacticks “. In other words the double quotes understand whatever after a dollar sign as a variable.
  • Backticks: Creates a shell within a shell. Thus, shell runs the command and captures STDOUT back into a variable. A good example is with the command date. For example, you can type:
datenow='the date now is `date`'

echo datenow
The date is Sat Apr  4 20:16:43 DST 2020

An alternative way to call as shell within a shell, and in our example the date is by typing:

datenow="the date now is $(date)"

echo datenow

Numeric Variables in Bash

We can add two integers with the following ways:

expr 4 + 6
echo $((4+6))
# only the bc method allows operations with decimal numbers
echo "4+6" | bc

Example: Build a bash script which takes as an argument the temperature in Fahrenheit and it converts it to Celsius using the formula:

C = (F – 32) x (5/9)

Let’s call our script and get the degrees in Celsius of 100 Fahrenheit

# Get first ARGV into variable

# Subtract 32
temp_f2=$(echo "scale=2; $temp_f - 32" | bc)

# Multiply by 5/9 and print
temp_c=$(echo "scale=2; $temp_f2 * 5 / 9" | bc)

echo $temp_c

Call the script:

bash 100

And the output that we get:


Arrays in Bash

There are two types of arrays in Bash:

  • An array
    • ‘Normal’ numerical-indexed structure.
    • Called a ‘list’ in Python or ‘vector’ in R.
  • An associative array
    • Similar to normal array, but with key values pairs, not numerical indexes
    • Similar to Python’s dictionary or R’s list
    • Note: This is available in Bash 4 onwards

You can create an array in two ways:

# declare without adding elements
declare -a my_array

# create and add elements at the same time
my_array=(1 2 3)

Remember- no spaces round equal sign and no commas between elements!

You can return all array elements using array[@] . Note that Bash requires curly brackets around the array name when you want to access these properties. You can access an array element using square brackets. Notice that bash uses zero-indexing for arrays. You can append arrays using array+=(elements). Example:

my_array=(1 2 3 10)
echo ${my_array[@]}

# the length of the array
echo ${#my_array[@]}

# access the four element of the array
echo ${my_array[3]}

# change the first element of the array to 5
echo ${my_array[0]} 

# slice an array N is the starting idex and M is how many elements to return my_array[@]:N:M
echo ${my_array[@]:1:2} 

# append an element
echo ${my_array[@]}
1 2 3 10
2 3 10
5 2 3 10 20

Examples with associate arrays:

# Create empty associative array
declare -A city_details
city_details=([city_name]="New York" [population]=10000000)

echo ${city_details[city_name]} #index using key to return a value

# return all the keys
echo ${!city_details[@]}

New York
city_name population

IF statement

A basic IF statement in Bash has the following structure:

if [ condition ]; then
    # some code
    # some code the else statment is optional

Notice that there are spaces between square brackets and conditional elements and there is a semi-colon after close-bracket ];

  • = is equal
  • != is not equal
  • > is greater
  • < is less
  • -eq is equal
  • -ne is not equal
  • -lt is less than
  • -le is less than or equal to
  • -gt is greater than
  • -ge is greater than or equal to
  • -e if the file exists
  • -s if the file exists and has a size greater than zero
  • -r if the file exists and is readable
  • -w if the file exists and is writable
  • $$ for AND
  • || for OR

For more conditional flags you can have a look here.


Assume that there is a folder called employees/ with a text file for every employee of the form:

Name: George Pipis
Hiring Date: 2010
Salary: 30000
Department: Data Science

You want to write a script which will take as an input the corresponding txt of the employee and will move it to a new folder according to Hiring Date. If the Hiring Date is greater or equal to 2018 then will go to the new_employee/ folder, else to the old_employee/ folder.

Run the script for the George_Pipis.txt



# Extract Hirining Date from first ARGV element
hd=$(grep Date $1 | sed 's/[^0-9]*//g')

# Conditionally move into new_emplyees folder
if [ $hd-ge 2018 ]; then
    mv $1 new_employees/

# Conditionally move into old_emplyees folder
if [ $hd-lt 2018]; then
    mv $1 old_employees/

And we run it:

bash employees/George_Pipis.txt

FOR Loop in Bash

The basic structure in Bash is:

for x in 1 2 3
    echo $x

You can also use the brace expansion which is {START..STOP..INCREMENT}

for x in {1..5..2}
    echo $x

There is also the three expression syntaxt

for ((x=2;x<=4;x+=2))
    echo $x

Glob expansions

for employee in employees/*
    echo $employee

Shell-within-a-shell to FOR loop

We could loop through the result of a call to shell-within-a-shell:

for employee in $(ls empoyees/ | grep -i 'georg')
    echo $employee



You want to write a script where it prints out all the files which end with .py from the folder my_code/


for file in my_code/*.py
    echo $file


You have many Python scripts in a folder called my_python_tests/ and some of them are about text mining tasks. You can assume that all Python scripts which contain the code import re can be classified as text mining tasks. Your task is to write a Bash script which will move all the text mining scripts to a folder called text_mining/.


The can be as follows:

# Create a FOR statement on files in directory
for file in my_python_tests/*.py
    # Create IF statement using grep
    if grep -q 'import re' $file ; then
        # Move wanted files to text_mining/ folder
        mv $file text_mining/

WHILE statement syntax

Iterations continue until this is no longer hold.

  • Use the word while instead of for
  • Surround the condition in square brackets
    • Use of same flags for numerical comparison from IF statements
  • Multiple conditions can be chained or use double-brackets just like IF statements along with $$ , ||
  • Ensure your loop terminates!
while [ $x -le 3 ];
    echo $x

Build a CASE statement

The structure of the CASE statement is:

case 'STRING' in

Let’s write a Bash script which will take as an input a string and will return this it is a weekday, a weekend or not a day.

# Create a CASE statement matching the first ARGV element
case $1 in
  # Match on all weekdays
  echo "It is a Weekday!";;
  # Match on all weekend days
  echo "It is a Weekend!";;
  # Create a default
  echo "Not a day!";;

bash Monday
It is a Weekday!


The folder my_r_scripts contains R script from R Machine learning models. Our task is to move the tree based models to a file called tree_models/ and to remove the KNN and Logistic regression models.


We will use the CASE statement

# Use a FOR loop for each file in 'model_out/'
for file in my_r_scripts /*
    # Create a CASE statement for each file's contents
    case $(cat $file) in
      # Match on tree and non-tree models
      *"Random Forest"*|*GBM*|*XGBoost*)
      mv $file tree_models/ ;;
      rm $file ;;
      # Create a default
      echo "Unknown model in $file" ;;

Functions in Bash

The syntax of Bash functions is:

function name () {
    # your code
    return #return something

Or alternatively

function function_nmae {
    # your code
    return #something

Example: Let’s write a function that iterates over python files in a folder and prints out their names.

# Create function
function print_file_names () {
  # Loop through files with glob expansion
  for file in myfolder/*.py
    # Print the file name
    echo "I found this file: $file in the folder"

# Call the function


Write a Bash function in a bash script which returns the current day.


# Create function
what_day_is_it () {

  # Parse the results of date
  current_day=$(date | cut -d " " -f1)

  # Echo the result
  echo $current_day

# Call the function


Passing arguments into Bash functions

You can pass arguments into functions using the $1 , $2 notation as we saw earlier. Also, you can use the [email protected], $* and $#


Create a function that takes as inputs two numbers and it returns their ratio.

# Create a function 
function return_ratio() {

  # Calculate the percentage using bc
  ration=$(echo "scale=4; $1 / $2" | bc)

  # Return the calculated ratio
  echo $ration

# Call the function with 456 and 632 and echo the result
return_test=$(return_ratio 456 632)
echo "456 out of 632 as a ratio is $return_test"


Write a function that sums up an array:

# Create a function with a local base variable
function sum_array () {
  local sum=0
  # Loop through, adding to base variable
  for number in "[email protected]"
    sum=$(echo "$sum + $number" | bc)
  # Echo back the result
  echo $sum
# Call function with array
test_array=(14 12 23.5 16 19.34)
total=$(sum_array "${test_array[@]}")
echo "The sum of the test array is $total"
The sum of the test array is 84.84

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Leave a Comment

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore

monte carlo integration in python

Monte Carlo Integration in Python

We will provide examples of how you solve integrals numerically in Python. Let’s recall from statistics that the mean value

3-day flash sale. Online courses start at $11.99