FAQ: Learn Bash Scripting - Conditionals

This community-built FAQ covers the “Conditionals” 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 Conditionals

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!

Hey!
I wonder why in in stage 3 “Conditionals” there is “greeting_number=0” prewritten in script.sh, yet in our conditional we are supposed to write “greeting_ocasion”. When I executed the file with “greeting_ocasion” there was an error.
Here is the code that is written by instructions and does not work properly:

#!/bin/bash
first_greeting=“Nice to meet you!”
later_greeting=“How are you?”
greeting_number=1

if [ $greeting_occasion -lt 1 ]
then
echo $first_greeting
else
echo $later_greeting
fi

When script is executed in bash it displays “./script.sh: line 7: [: -lt: unary operator expected”

And here that works but is not written as asked:

#!/bin/bash
first_greeting=“Nice to meet you!”
later_greeting=“How are you?”
greeting_number=1

if [ $greeting_number -lt 1 ]
then
echo $first_greeting
else
echo $later_greeting
fi

Am I thinking wrong? If yes, please explain.

Cheers!
Simon

4 Likes

Yeah, this same thing happened to me. What’s going on?

3 Likes

This is a mistype, _number instead of _occasion. I took my hint from the error message, changed it and passed. Be sure to submit a bug report pointing out this glitch.

3 Likes

The problem for me was that I did not add the proper spacing within the square brackets of the for loop.
for [ greeting_occasion -lt 1 ] which is correct, instead of for [greeting_occasion -lt 1] which is not correct because there needs to be a space at the ends within the brackets.

11 Likes

Note that there should be spaces after the if:sunglasses:

2 Likes

The lesson text says “When comparing strings, it is best practice to put the variable into quotes ( " ).”

However, the lesson does not pass unless you enter the variable WITHOUT quotes around it.

This seems illogical. Why tell us that something is best practice if it’s not allowed in the lesson? Is there something I’m missing here?

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

The variable getting compared (greeting_occasion) is set to a number, not a string.

1 Like

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

When I run this code in the terminal with ./script.sh I get an error “command not found” at line 6

1 Like

I have the same question. my guess is that when a string is placed after an echo command, the command line is already expecting a string so qoutes is too much information and with the if conditional it is precising what to compare. i guess?

I’m not sure, but I’m guessing it’s because of the missing indentation?

"The script files also need to have the “execute” permission to allow them to be run. To add this permission to a file with filename: script.sh use:

chmod +x script.sh"
It says here that this command is needed to allow a script file(which is a file containing a list of commands) to be executed.
But in this exercise , i was not required to enter the “chmod” command.
For my script.sh to run , all i had to do was type “./script.sh” in the terminal.
When and where is the “chmod +x script.sh” command really required?

2 Likes

Might be a different problem, but I was putting a space between “=” and the number in the variable line.

This works: “greeting_occasion=0”
This did not work: “greeting_occasion= 0” and yielded the error message “command not found” on line 4.

Hi cassi2394,
I found this post on StackExchange that answers your question, especially the segment on the Bourne family, as it concerns our bash command line. It shows the differences between the different commands, depending on where you put the space.

All the best,

Thanks! Wow, one must be so precise with spacing!

1 Like

can’t get why I have the message

You didn’t write this line:
greeting_occasion=1

#!/bin/bash
first_greeting="Nice to meet you!"
later_greeting="How are you?"
greeting_occasion=1
if [ $greeting_occasion -lt 1 ]
then
  echo $first_greeting
else
  echo $later_greeting
fi
$ ./script.sh
./script.sh: line 6: [1: command not found
How are you?

getting this error @mtf

Hello @safealiahmed90817081!

Try setting greeting_occasion = 0 instead of 1.