Bash Scripting — [TryHackMe]

gr33nm0nk2802
4 min readJun 12, 2021

Bash is a scripting language that runs within the terminal on most Linux distros, as well as MacOS. Shell scripts are a sequence of bash commands within a file, combined together to achieve more complex tasks than simple one-liner and are especially useful when it comes to automating sysadmin tasks such as backups.

Difficulty: Easy

Link: https://tryhackme.com/room/bashscripting

Task #1: Introduction

Throughout this room you will learn various concepts like:

  • Bash syntax
  • Variables
  • Using parameters
  • Arrays
  • conditionals

An awesome cheat sheet for bash scripting has been provided here.

https://devhints.io/bash

Task #2: Our first simple bash scripts

In order to write our first simple bash script, we must understand a few basics.

  1. Every bash script has a shebang at the start of it. (#!/bin/bash)
  2. Followed by this shebang line we have the set of commands that we wish to execute
  3. Generally bash scripts have an extension of (.sh)
  4. After creating our script give it executable permission using

chmod +x [fielname].sh

Then we can execute the script by using ./filename.sh

Q. What piece of code can we insert at the start of a line to comment out our code?

#

A hash is used to mark or comment out a given line.

Q. What will the following script output to the screen, echo “BishBashBosh”

BishBashBosh

Task #3: Variables

Variables are simply used to store values. We use the assignment operation to store values and in order to use them we must use the $ sign.

We also sometimes need to debug the code. To debug the code we use

bash -x ./myscript.sh

In order for this to work we must set the sections where the code is to be debugged.

You can see its outputting a + for the command and then the output of what that command executed. If there was an error it would output a on that line this makes it easy to spot where you have gone wrong so you can fix them.

Q. What would this code return?

Jammy is 21 years old

Q. How would you print out the city to the screen?

echo $city

Q. How would you print out the country to the screen?

echo $country

Task #4: Parameters

We can pass arguments to the bash scripts in two ways.

  1. Command Line Arguments
  2. User Input

In order to pass the arguments as command line input, we use the $N parameter where N is the count of the parameter. For example:

Also if we wanted to pass the arguments to script, we can use the read command followed by the variable name.

Q. How can we get the number of arguments supplied to a script?

$#

Q. How can we get the filename of our current script(aka our first argument)?

$0

Q. How can we get the 4th argument supplied to the script?

$4

Q. If a script asks us for input how can we direct our input into a variable called ‘test’ using “read”

read test

Q. What will the output of “echo $1 $3” if the script was ran with “./script.sh hello hola aloha”

hello aloha

Task #5: Arrays

Q. What would be the command to print audi to the screen using indexing.

echo “${cars[1]}”

Q. If we wanted to remove tesla from the array how would we do so?

unset cars[3]

Q. How could we insert a new value called toyota to replace tesla?

cars[3]=’toyota’

Task #6: Conditionals

Conditional statements are used to execute a statement based off some conditions.

Q. What is the flag to check if we have read access to a file?

-r

Q. What is the flag to check to see if it’s a directory?

-d

Peace Out

--

--

gr33nm0nk2802

A cybersecurity enthusiast interested in Reverse Engineering. Love to Code