Bash Scripting - Write-up - TryHackMe

Information

Room#

  • Name: Bash Scripting
  • Profile: tryhackme.com
  • Difficulty: Easy
  • Description: A Walkthrough room to teach you the basics of bash scripting

Bash Scripting

Write-up

Our first simple bash scripts#

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

Answer: #

It's the same character as in most languages but if you don't know you can read the room material.

What will the following script output to the screen, echo "BishBashBosh"

Answer: BishBashBosh

echo just print the string passed as argument.

Variables#

What would this code return?

Answer: Jammy is 21 years old

You can write the script or evaluate the variables in your brain.

How would you print out the city to the screen?

Answer: echo $city

Simply echo the variable.

How would you print out the country to the screen?

Answer: echo $country

Simply echo the variable.

Parameters#

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

Answer: $#

Check the cheatsheet.

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

Answer: $0

Check the cheatsheet.

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

Answer: $4

Check the course material.

If a script asks us for input how can we direct our input into a variable called 'test' using "read"

Answer: read test

The answer is in the question.

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

Answer: hello aloha

It will display 1st and 3rd args.

Arrays#

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

Answer: echo ${cars[1]}

The second string is at index one.

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

Answer: unset cars[3]

Use unset to remove an item.

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

Answer: cars[3]=toyota

Set a string at index 3.

Conditionals#

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

Answer: -r

The first letter of read.

What is the flag to check to see if it's a directory?

Answer: -d

The first letter of directory.

Share