FAQ: Learn Bash Scripting - Loops

#1

This community-built FAQ covers the “Loops” exercise from the lesson “Learn Bash Scripting”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Web Development

Learn the Command Line

FAQs on the exercise Loops

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

#2

I didn’t understand any of this lesson. I had no idea what to do and all of the words didn’t make any sense. The only thing I finally understood was the solution, but that was after I had given up on the exercise. Given the little knowledge in programming that I have, how was I supposed to know how to do what the lesson was asking me to do?

8 Likes
#3

I agree, the lesson in no way explained nor gave an example on “how to” nest a conditional “if” statement within a while loop. It was inferred that running an echo command was no different than including an if conditional within the while loop. I don’t believe someone trying to learn Bash would “just” pick up on that.

This is problematic with tutorials, at least so I have found, where Bash, Linux etc are concerned. Sometimes programming as well, it is done in a way that is not very beginner friendly. There are terms and behaviors that do not always get very well defined.

Perhaps a better explanation of what loops are, how they work and how/when to use them would be beneficial before doing an exercise on nesting conditional within conditionals. It may also be nice to see an example of the nesting of one conditional into another before asking a novice to perform.

I have been a Linux user for years, written plenty of Bash scripts and to be quite honest the way this exercise was presented was a bit confusing.

7 Likes
#4

I agree with both of the above users. I have been scripting for a while now and the way these instructions are written would be very confusing for a beginner and just confusing in general. Unless you know about nesting if then statements into loops, this exercise will just be frustrating and make the user want to quit.

4 Likes
#5

I’d also agree with this sentiment. This is a nice Bash refresher, but this would be extremely tough without some Linux and Bash knowledge already ingrained. The if statement provided in this lesson does not even provide indentation which will increase confusion.

Similarly, I’m not sure how the answers are being calculated. It does not look to be comparing STDOUT in the terminal, as my results are the expected output yet the solution states it’s incorrect. It’s a nice start but these could be fine tuned some.

3 Likes
#6

I have to agree with everything that has been stated. I want to be able to apply what I have learned in real-life situations. Understanding conditions, loops and how they work is important to writing meaningful scripts. Not to mention, they lay the ground work when utilizing them in other programming languages. This lesson needs to be reviewed and adjusted from a Beginner perspective.

4 Likes
#7

Just wanted to add my voice to the group of people stating their confusion and frustration at this exercise. I managed to complete it, but I have no idea what I just did and why.

Compared to the Python 3 course, the Command Line course feels like an afterthought and the way some lessons are laid out are weird as heck. The instructions will ask you to write and run some commands without knowing what they are or what they do, THEN they will explain what you have just done. Wouldn’t it be better to begin by explaining some basic principles, asking the user to perform the task and then review the material again to make sure it sinks in properly?

9 Likes
#8

My thoughts exactly. Before starting Python3, I had absolutely no coding experience, but was eventually able to figure everything out even if i had to restart a lesson. With the command line lessons, I always have to do them twice and still do not feel I am getting enough repetition for all the the concepts to stick long term.
My goal is to be able to use what I learn in a conversation with the computer, not to buzz through a course “by the end of the semester.”
Maybe our feedback will alter the design of the CLI course

2 Likes
#9

Super NOT impressed with this particular lesson. Way too much assumed by the writers on what the users know/don’t know about CLI.

1 Like
#10

Lesson 4 of 7, Bash Scripting Loops

I think I had the correct solution on my own. I just wish I had copied my solution prior to asking for the solution so I could compare the two.

Funny (bug?) when I ran the script, I think the output was wrong. It prints the first greeting and then the second greeting twice (three lines in total).

#11

I 100% agree with this. I’ve been staring at this lesson for days and with no prior knowledge of programming before the Python 3 course, and this is extremely confusing for me.

#12

Lesson 4 (loops), ex 1 has a bug on line 5, or what seems to be a bug to me:
while [ $greeting_occasion -lt 3 ] #this works
while [$greeting_occasion -lt 3] #this does NOT work

Is bash really this picky about white space?!

#13

Reading all these comments even from people with programming experience makes me feel a million times better as a total beginner who was wondering if I was just plain stupid :sweat_smile:

3 Likes
#14

I didn’t even start with this lesson, instead I tested the if/then/else statement. My code is as follows:

#!/bin/bash
first_greeting="Nice to meet you!"
later_greeting="How are you?"
greeting_occasion=2
if [ $greeting_occasion -lt 1 ]
then
  echo $first_greeting
else
  echo $later_greeting
fi

The only thing I’m changing is the value of greeting_occasion, then I type ./script.sh into the terminal and the output is always “Nice to meet you!”, even if greeting_occasion equals 1 or more. To be honest, I don’t see a point in contiuning if not even the if/then/else statement works properly… Or am I missing something here? Is there a typo or anything in my code?

#15

There are three boxes on your page. First box: Instruction; Second: script file; Third, terminal;

The only reason I can think of is, you did not run the second box - There is an orange button for you to update your script file.
Write your code in script file >> Update your file >> terminal run: “chmod +x ./script.sh” >> terminal run: “source ./script.sh”

According to your code, you are doing the previous assignment. The result is supposed to be “How are you?” Let me know if it works!

2 Likes
#16

Wow, that was the problem! Thank you!

I didn’t know I needed to press “Run” every time I make a edit in my code. I think this wasn’t explained anywhere… I’m happy that the community is so kind :smile:

#17

Yeah, they could have explained it better. I did not know either, hahaha.

Don’t know about the community. Just felt like helping :wink:

1 Like
#18

I don’t recall this lesson stating exactly how to go about nesting conditionals. I got frustrated and took the solution, compared to my code and the only difference is that mine did not contain the indentations. How many spaces are we to use 2? 4? Use the tab key? In the provided solution they indent 2 spaces. Make sure you follow that convention going forward…

while …
do
–if …
–then
----echo…
–else
----echo…
–fi

done

Frustrating for sure.

1 Like
#19

@danieljc8888 thanks a lot for the explanation of what I should write in the terminal to be able to run script.sh Now I can finally see the output. Before I didn’t get it and was doing:

$ ./scrpit.sh
bash: ./scrpit.sh: No such file or directory

Now, thanks to you all good:

$ chmod +x ./script.sh
$ source ./script.sh
Nice to meet you!
How are you?
How are you?
$

Good stuff. Thanks for helping :slight_smile:

2 Likes
#20

What’s the function of “in” within the for conditional statement?

I didn’t saw an explanation on what does “in” do, i appreciate you to explain it to me.

Thanks.