When you’ve got prior expertise of programming and you might be simply getting began with bash, there’s one factor that you could be discover a bit odd.
In contrast to different programming languages, capabilities in bash don’t return any worth. As an alternative, it returns exit standing or a numeric worth indicating the success or failure of execution.
However the query right here is the way you make the perform return worth in bash.
Certain, if I am scripting this tutorial, I will need to have an answer (learn workaround) to sort out this challenge however earlier than that, let’s perceive the issue first.
The issue we’re coping with
First, let me share a easy C program to clarify how programming languages return worth by means of a bash perform:
#embrace <stdio.h>
// Operate declaration with return sort int
int add(int x, int y) {
return x + y;
}
int most important() {
// Name the perform and retailer the lead to a variable
int end result = add(3, 4);
// Print the end result
printf(“%dn”, end result);
return 0;
}
If you happen to run the above C program, it’s going to return 7 as proven right here:
Within the above C program, I created a perform named add that takes two values and makes use of the return assertion so as to add two values.
Sadly, capabilities in bash don’t work that approach and what they provide you is the exit standing of the final command used within the perform and the exit standing may be 0 (success) or non-zero (failure).
To know the exit bash, there is a particular variable within the bash $? to know the exit standing. So let me rapidly share how you should use it:
#!/bin/bash
# Defining the perform
perform take a look at {
echo “A demo perform created to examine the exit standing”
}
# Calling perform
take a look at
# Checking the standing
echo “The exit standing of the take a look at is: $?”
As you may see, it returned 0 indicating the perform was executed efficiently!
Now, let’s take a look at how one can make the bash perform return worth.
The right way to make bash capabilities return a worth
There are two methods wherein you can also make the bash capabilities return a worth:
Utilizing the usual output information stream (includes utilization of echo or print command)Utilizing world variable
After all, it is not the identical as returning values in C or different programming languages however it could assist you in lots of circumstances.
So let’s begin with the primary one.
Technique 1: Utilizing the usual output information stream
In case you are unaware of knowledge streams, there are 3 information streams in Linux: enter, output, and error. Every is supposed for a distinct job.
Because the title suggests, I’ll use the usual output information stream utilizing the echo or the printf command on this technique.
Nonetheless confused? In easy phrases, I will likely be sending the output of the perform to the usual output information stream to print the output (there’s the place the utilization of the printf or echo command).
First, let me share a bash script:
#!/bin/bash
# Operate definition
perform demo {
native namaste=”Namaste from Bash!”
echo “${namaste}”
}
# Name the perform and seize its output
output=$(demo)
# Print the captured output
echo “${output}”
When executed, it gives you the next output:
Now, let’s take a look on the clarification half.
First, I created a perform demo wherein there was an area variable namaste containing the string that I needed to print.
Later, I used the echo command to print the info within the namaste variable.
Within the subsequent half, I referred to as the perform within the $() notion which is able to execute the demo perform within the subshell and the output is captured by the output variable.
Lastly, the echo command was used to print the captured output of the ouptut variable.
Fairly simple. Proper?
Technique 2: Utilizing a worldwide variable
By default, each variable within the bash is world and to make use of the native variable, it’s important to use the time period native earlier than assigning any worth to it.
However the query is how you employ a worldwide variable to return worth within the bash perform. Properly, let me present a easy bash script that makes use of a worldwide variable for that function:
#!/bin/bash
# World variable to retailer the end result
output=””
# Operate definition
perform generate_greeting {
native title=$1 # Operate parameter
# Assign the greeting to the worldwide variable
output=”Howdy, ${title}!”
}
# Name the perform with a reputation as an argument
generate_greeting “$USER”
# Entry the worldwide variable
echo “${output}”
The above script merely greets the at the moment logged-in person and whenever you execute the above script, you may anticipate the next output:
Now, let’s break down the script.
First, the empty world variable output was declared.
Then, the perform named generate_greeting was created that takes one parameter title and within the subsequent line, the greeting textual content with username was assigned to the output variable.
Subsequent, the script will name the perform generate_meeting with the $USER argument to get the title of the at the moment logged-in person.
Lastly, utilizing the echo command the worth of the worldwide variable output was printed.
Yep, that is all it takes you to make use of a worldwide variable to make a perform return worth in bash.
Which technique to make use of? (in my view)
I personally do not like the concept of utilizing a worldwide variable to make the bash capabilities return a worth and there is a easy purpose why.
World variables by their nature may be accessed and modified wherever within the script which can trigger bother, particularly if the script is complicated and it could be modified by the person in between.
So what I would suggest is to make use of the primary technique, which makes use of the usual output information stream.
I hope you’ll discover this information useful.