FAQ: Learn Bash Scripting - Review

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

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!

The BASH CLI course is far too narrow and, AFAIC, incomplete.

3 Likes

It really seems like this Bash Scripting course stops short. Can somebody tell me about, for example, functions?

I’ve done some research, but I can’t get my function to return anything. When I call the function, instead of returning a number, it apparently returns the name of the function.

Here’s my code:

#!/bin/bash

get_number() {
	return 6
}

boolean_number=get_number
echo $boolean_number

Out of the code above, all I get in the terminal window is get_number

Can someone help me, please?

2 Likes

That’s not the name of the function, that’s just text which happens to be the same characters as the name of the function. You’d need to evaluate that expression to obtain its result.

You might also want to write the result of your function to stdout rather than setting an exit value.

1 Like

Well, how do I properly call the boolean_number function and assign it’s return value to the boolean_number variable?

Also, can you confirm that I don’t have any syntax errors in my function?

1 Like

You…don’t. They’re not conventional functions. They’re more like programs.
If you wanted such behaviour you should likely be looking for something like perl/python/ruby/whatever.

bash isn’t a programming language as much as it is just glue between other programs
You can certainly encode logic with it but err. Yeah. That’s not quite what it is.

1 Like

So, the first thing to note is that your expectations don’t apply.

But you can get similar behaviour. Like mentioned, you’d probably print the result instead. You can also set a variable which the caller can look at afterwards.

add() {
    echo "$1 + $2" | bc
}

echo $(add 1 2)

add() {
    returnvalue=$(echo "$1 + $2" | bc)
}

add 1 2
echo $returnvalue
unset returnvalue

Oh and getting quoting right is a bit of a nightmare so, again, you might want to prefer to just not, and instead use some… uh. you know. programming language.

1 Like

Oh. Okay. That’s too bad

1 Like

Hello to everyone!

Could someone please be so kind as to explain some things to me?

So far I had no trouble understanding other lessons but this one in particular I cant understand. For example I don’t understand the meaning behind this part of code “index=$((index + 1))”…why do we write that, what is its purpose, why “+1”…Other lessons breakdown each command word by word and explain what each word does basically, what is the logic behind it. Here I have no clue what this means and what is its purpose. As a result, I do not understand the result that is produced from the code we build in this chapter.

while [ $greeting_occasion -lt 3 ]
do
if [ $greeting_occasion -lt 1 ]
then
echo $first_greeting
else
echo later_greeting fi greeting_occasion=((greeting_occasion + 1))
done

Why does it produce just 2 “How are you?” statements if this is a loop? the variable does not change (remains equal to 1). If I am not mistaken the while loop basically loops the same output over and over again as long as the variable meets a certain condition.

Then we go to Inputs…here I cant even begin to understand what I want to ask…The exercise tells us to get user input and assign it to the variable greeting_limit…and replace the number 3 with that variable(prepended $)…is ‘read’ adopting whatever ‘echo’ follows? I am lost…

Please if someone could shed some light for this particular last chapter(5) of Learn command Line… I wish I could ask better questions but I am too confused. And again this is the first time I felt this way into a lesson.

Thank you in advance!

1 Like

I agree. The course was great, and provided explanations for each command as well as how it might be useful when coding IRL. But this Bash section just confused me, and I can honestly say that I have no idea why I did what I did, what the purpose of the commands is exactly IRL, and how input arguments, aliases, conditionals, loops, or anything else in this section, fits together. This was horrible. And it began at the beginning with the first command: #!/bin/bash (pray tell what does the “#” do, or even the “!”). This was frustrating, and not at all worthy of being approved for beginners.

3 Likes

Glad to see I am not the only one who struggled with this…I was so frustrated and was starting to believe I am a dummy hahaha

Just had the exact same thoughts. Spent a few days working through the course and understanding it all more or less, but the bash scripting section referenced so many things without telling us what they are, what they mean or why they did them. Suddenly felt like I skipped ahead 10 lessons.

Hi

Okay, so I have a couple of questions regarding the purpose of bash scripts. The tutorial states that all code that can be run in terminal can also be run using a bash script, so I don’t quite get why they need both… What do bash scripts have that terminal doesn’t have?

Also, there seems to be some connection between bash scripts and the ./bash_profile that I don’t understand… Is this correct or am I overinterpreting?

Lastly, could someone please explain to me what the purpose of the read command is (btw, if there is an acknowledged guide explaining all of the function, it would be awesome if you could share that). It says in the tutorials that it “prompts the used for input in order to access data external to the bash script file itself”. - I do not understand what that means. Below is the example that they gave in the exercise:

echo “Guess a number”
read number
echo “You guessed $number”

Thanks in advance

A script is code stored in a file for later use.
If you just want to fire off some one-off commands then you’d have no reason to store the commands in a file.
On some systems bash is analogous to your mouse pointer, it is how the computer is interacted with. For some systems there isn’t even a choice, they aren’t plugged to a monitor and therefore don’t draw pretty pictures for anyone to click on.

Many programs come with manuals, if you run

$ man read

On a system that has both man and manpages installed then you’ll find a description on what it does…You can also google for that manpage.

.bash_profile and .bashrc can be used to run commands every time bash is started, think of it as settings, except there are no settings, instead you run commands to modify the environment to your liking.

bash’s man page has information about them.

$ man bash