TryHackMe: Bash Scripting

64bitCoder
3 min readApr 26, 2021

Hello Everyone! This Free Walkthrough room in TryHackMe was created by fieldraccon . An awesome ,GOD level user of the platform. This room is designed to teach us the basics of Bash Scripting.

  1. Are you ready to go!

Ans. No answer needed.

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

Ans. #

“#” is inline Bash comment.

Example:

# This is a Bash comment.
echo "This is Code" # This is an inline Bash comment.

Exception: The only exception to this rule is when the first line on the script starts with the #! characters.

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

Ans. BishBashBosh

Echo command displays the specified message to the screen.

4. What would this code return?

Ans. Jammy is 21 years old

The variables “name” and “age” correspond to “Jammy” and “21” respectively. Therefore, the code will display the value which corresponds to the above variables when echo command is used.

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

Ans. echo $city

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

Ans. echo $country

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

Ans. $#

$# stands for counts of the number of the positional parameters passed to a script.

For example:

example.sh 1 2 3 4 5 , there are 5 parameters as passed so the count of the $# is 5.

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

Ans. $0

$0 is set to expand the name of the shell or shell script. It is pre-defined.

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

Ans. $4

The general form to get a desired argument is $X where “X” is the number of the argument supplied.

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

Ans. read test

read is a bash built-in command that reads a line from the standard input.

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

Ans. hello aloha

As per the command, only argument number 1 and 3 are to be displayed. Therefore, among the variables “hello hola aloha” only hello (1st variable) and aloha (3rd variable) will be displayed.

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

Ans. echo "${cars[1]}"

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

Ans. unset cars[3]

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

Ans. cars[3]='toyota'

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

Ans. -r

The -r flag checks if the file is readable.

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

Ans. -d

The -d flag checks if the directory exists.

Thankyou guys! Happy Scripting :)

--

--