FAQ: Learn Bash Scripting - Loops

I am side-by-side doing these exercises in my own Ubuntu script.sh file.
When I put the initial “while” example in, it tells me it’s expecting a unary operator around the “-lt”

Any ideas?

1 Like

Same thing happened to me. I kept getting the error message telling me to nest the if…fi in the do, and to increment. After changing it multiple times, indenting the if…fi part as many different ways I could think of, I tried running the script in the terminal too. It didn’t work. The given solution looked exactly like how I thought I had it, which made feel better. I, too, which I had copy/pasted what I had, because it looked to be the same.

1 Like

You got that right, I been coding for 2 months and sometimes my mind goes blank as to what the exercises are asking, but I take a step back for a break, reassess and come back to keep trying. it is difficult but not impossible. Keep it up, coding is more than a set of instructions, it is an art form

The Khan Academy tutorials go more into depth with this particular concept. I advise doing those first, then coming over here and completing these. Each site complements the other, to give you a more complete understanding.

Hi there! I just jumped in this lesson!
I’m a beginner. How come nothing happens in Terminal! What do we have to put in Terminal ?

Basically loop is a function which helps us perform tasks which are repeatative and also have some pattern associated to them, without writing the statement again and again.
For eg- If we have to print numbers from 1 to 10, i.e “1 2 3 4 5 6 7 8 9 10”, we can do it by writing echo statement ten times and then increasing the value of the variable being used after every echo statement as shown below:
“count = “1”
echo count count = ((count + 1))
echo $count
…and so on ten times.”
, or we can just use the loop statements, i.e “for, while or until”. as given below:
“count = “1”
while [ $count -le 10]
echo count count=((count + 1))
Here the loop statement works in three steps
Step 1: while [ count -le 10] - This checks whether the value of count variable is less than or equal to 10, which is true in this case, so it proceeds to step 2. Had it been false, entire loop statement would've been skipped and command would've been transferred to the line after the statement "done". Step 2: do - performs a given tasks if the condition is true. The task in this case is printing the value of count variable. Step 3: Increment "count=((count + 1))" - increases the value of variable count by 1. Now the value of count is “2”.
After this the loop statement takes the command back to step 1. The difference is that now the value of the variable count is “2”.
Now after repeating this ten times, the value of the variable count is now “11”. Now the condition check statement gives false output and the loop comes to an end after printing the numbers from 1 to 10.
This saves us from manually printing and increasing the value of count variable ten or twenty times.

to all those people who didn’t get it,
ask yourself why would you jump straight to terminal?
medical doctors don’t perform operation right aways…
people don’t realize that .BASH chapter is №8 of 14 WEB PATH

This lesson is garbage. I had no idea what I was doing

1 Like

Hey @webace16380! Welcome to the forums!
Can you please tell us what was wrong with the lesson. If you feel something didn’t make sense or wasn’t explained well, you can submit a bug report explaining what you think should be changed. Telling us just that the lesson is garbage doesn’t really help.


Whelp, I guess this is where my time with codecademy ends.
I pretty much flew through the previous lessons, but while it was getting harder, this is where everything fell apart. Even with the answers provided, I couldn’t even put them in the right place. Good thing I got this far without paying for a membership. :roll_eyes:

1 Like

This is not a very helpful comment, if we are not supposed to jump straight into the terminal training, then why does Codecademy give us the option to?
There is no guidance given to let novices know when is the right time to take a course!

1 Like

When I run the while code in bash I get the following:
./script.sh ./script.sh: line5: [: 0 -lt: unary operator expected

But I get the green tick box in the instructions window?

FYI this is my code:
while [ $greeting_occasion -lt 3 ]
if [ $greeting_occasion -lt 1 ]
echo $first_greeting
echo later_greeting fi greeting_occasion=((greeting_occasion +1))

Can anyone help please?

1 Like

I am having trouble with the same question and have the same code. Why is this wrong?

This Loop exercise is terrible.

Hi @object4812586542!

What are you having trouble with?

I have to agree with the folks above. This lesson is confusing and not well written.

I’d recommend submitting a bug report. The more bug reports saying that this lesson is confusing, the faster the people working at CodeCademy will see the bugs.

Will do!
Thanks for the fast reply!

1 Like

Agreed, this was very confusing. It needs to be better written to explain how to properly nest the “if” part into the “while.”