THM Bash Scripting (A Walkthrough room to teach you the basics of bash scripting) by Razrexe

RAZREXE
3 min readAug 18, 2021

A Walk-through room to teach you the basics of bash scripting

Task 1 : Introduction

What is bash?

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-liners, and are especially useful when it comes to automating sysadmin tasks such as backups.

These are a few things among many that you will learn in this room:

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

Throughout this room feel free to work along with me! You can test out the commands shown or integrate them into your own projects, after all, you learn by practicing and applying what you have learned into your own scenarios. Make sure to spawn the tryhackme attackbox or use your own terminal.

I found this website very useful when I was on my journey of learning bash, feel free to use it to help you through this room and for further learning after you finish! https://devhints.io/bash

Task 2 : Our first simple bash scripts

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

Ans: #

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

Ans: BishBashBosh

Task 3 : Variables

  1. What would this code return?

Ans: Jammy is 21 years old

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

Ans : echo $city

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

Ans : echo $country

Task 4 : Parameters

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

Ans: $#

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

Ans: $0

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

Ans: $4

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

Ans: read test

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

Ans: hello aloha

Task 5 : Arrays

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

Ans: echo “${cars[1]}”

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

Ans: unset cars[3]

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

Ans: cars[3]=”toyota”

Task 6 : Conditionals

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

Ans: -r

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

Ans: -d

Task 7 : Further reading

Hi there and thank you so much for completing this room on bash scripting! I want to congratulate you on persevering when things got tough and completing the room.

If you want to further extend your knowledge of bash or coding in general I really suggest the following websites:

Thanks again. If you want to ask me about anything or have any queries about the room you can reach me on Twitter https://twitter.com/razrexe

Good Luck on your bash journey!

Well done!

--

--

RAZREXE

Data engineer by profession with the skill set of a hacker, and a tech writer during tea breaks :)