Conditional Execution | Introduction to Bash Scripting

Hi all,

I’m completly lost. I feel like there is a whole bunch of stuff that I should have been taught in this section before they ask the question:

Create an “If-Else” condition in the “For”-Loop of the “Exercise Script” that prints you the number of characters of the 35th generated value of the variable “var”. Submit the number as the answer.

I’ve been given some starting code (below) which makes very little sense and I’m not quite sure where to start. It took my ten minutes just to figure out how to open and edit a .sh file in the first place, which isn’t mentioned at all in the Academy…

Any help anyone can offer, or ideas on where to start, would be great.




Count number of characters in a variable:

echo $variable | wc -c

Variable to encode


for counter in {1…40}
var=$(echo $var | base64)


I always like to start by listing the “asks”. I find that breaking it up can sometimes make it easier. Looking at the question:

  1. Create an If-Else condition inside the For loop.
  2. Stop at the 35th iteration. (35th time through).
  3. Print the number of characters of the variable generated.

Now work on each step individually, when you begin working through #1, it will help you figure out where you need to go.

  1. Create an If-Else condition inside the For loop.
    When you begin writing the If condition, you will then have to answer the question, what condition do I need to test? When the loop runs, what value or variable determines how many times it has run? Figuring that out will help you transition to #2.
# Count number of characters in a variable:
#     echo $variable | wc -c

# Variable to encode

for counter in {1..40}
        var=$(echo $var | base64)

DM me if you need a larger push.


Hiya, thanks for replying. I still feel like I need to do a separate course on BASH Scripting, as I don’t even know what the echo command means!! Is that like the print command in other languages?

It is similar. This course on Bash Scripting is pretty good. Scripting broken down is just a compilation of Bash instructions. So becoming competent with the Bash CLI will help you become better at scripting.

Did you already do the Linux Fundamentals course? Definitely check that out. If you have done it and want help getting better with the CLI. Maybe try working towards a certification like the LPIC-1. While the cert doesn’t really matter, the knowledge gained while attempting it is priceless.

When I began learning to use Linux, I uninstalled all other Operating Systems that I owned, and forced myself to only use Linux. Start light with Ubuntu or Debian, then adventure out to Arch. Hope this helps.

Thanks for the advice. Yeah, I’ve done the Fundamentals stuff, which was super hard as well, as someone who has never used Linux until I started all of this, so its a large learning curve. I can definitely see the benefit of knowing the CLI inside out; I’ll have a look at that LPIC-1 course, as this may be able to bridge the gap in assumed knowledge these HTB courses seem to assume! Thanks again

I am having similar issues. The tutorial information in this section is not helpful in guiding us to properly format the script. I have tried various ways of writing an if-else statement and it keeps giving me syntax errors. Are we supposed to make the if condition (counter=35), or is there something way different expected? Also, I’ve tried to write the script with the “shebang” in the first line, but it appears to be getting commented out because it begins with the # symbol. Perhaps some pseudo code could gently nudge us in a better direction.

1 Like

I finally figured it out! This is my first post on here so I hope its helpful! I was stuck on this for awhile myself. Check the comments in the example script, figure out how wc -c works. As previously mentioned you need to first print the 35th result and then count the characters and print that numerical value. Hopefully this helps! Don’t give up! Keep working it and remind yourself, “I just haven’t figured it out YET” not “I cant figure this out”

1 Like

I hear you! I’ve now found a course on udemy teaching bash scripting ( which I’m now doing so I have a better foundation of it before conitnuring, as I feel having a good grasp of this will be essential! Hopefully, once I’ve made my way through that, stuff like this will become a whole lot easier.

Hi Diagonal. Thanks for the tip on the wc -c. I’ll have a deeper look into it. I have found a StackOver thread with the answer, and I can’t connect the dots, so I’ve taken up a side course on Udemy on bash scripting, to give me a deeper understanding on what this all is!!

1 Like

Right on!!


This is the thread I found with the answer, but you also need to read the comments on it, as it needs some adjustment. I’m only putting it here so if someone else has the same problem and comes across this thread, there is a route to an answer, if they so desire…

This wordcount includes the end of line character, which is what is expected in the academy (even though it is not said anywhere…), the actual wordcount without it would be ${#var} instead of using wc


Hello! I`ve been trying to work this one with all the tips but I’m not capable!! So the linux course you mention they teach you what are you actually writing in the script?!?!

Hiya, the course I took on at Udemy is called Linux Shell Scripting: A Project-Based Approach to Learning

Honestly, it didn’t help me specifically with this challenge, and funnily enough, I even asked ChatGPT to give me the code and that never worked either and every result I enter gives me a different outcome. Very frustrating to the point of I’ve just moved on from it now and working on different modules, which is a shame!

Ohhhh… Unbeliavable…is that hard?? Some person from the discord tryed to help me but he told me it was letter to learn C++ first… the module is absolutely out of reach from someone without decent knowledge in scripting and programming… I just wanted to learn bash scripting…

Attention! Be care if you try to run your script from MacOs.
My head was close to blow because i was sure about my code was correct. But result i got was an incorrect number for the question. So i decided try on linux instance and resulting number (the correct one) is different from MacOs result with same script code.
I whish this info could help you.

I was about to lose my mind as well… and I was on MacOS. took my exact code to a linux box and it worked first time. THANK YOU! @jorgespejo
Now I’m gonna go learn WHY its different!

I still not understand. :sob:

Please help me!

Hey! Start by telling us where you are stuck, then we can point you in better direction.

hello all,

I spent several hours on this exercise myself and had a lot of trouble trying to understand it. I managed to solve it eventually but wanted to add some extra information to help others who might be like me and completely new to this topic!

  • the var is going to be encoded up to 40 times within this loop. The value therefore is going to keep changing because the var’s value is not reset or changed between iterations of the loop. This one part confused me massively at the start because I found myself asking “why would the var keep changing”?
  • we want to encode it only 35 times and print (echo) the base64 value to the terminal
  • I really struggled trying to create an if-else condition but found the solution was a lot simpler in the end but I am not sure it is what the exercise intended us to learn.
  • I suggest looking at adjusting the “counter in {1…40}” part to create your evaluation condition.
  • you can then use the function already given in the script’s clue to print and calculate the value.

Good luck!

1 Like