Detailed Explanation of the Return Value Problem of Processing Method in Shell



Nowadays, programmers who don’t know Linux don’t mean to say they are programmers, and those who don’t know shell programming can’t say they know Linux. With shell programming, when writing tools for batch processing, you often need to customize functions. In more complex cases, it may be necessary to return a value.

Because in the shell world, unlike other programming languages, it does not support methods that we are familiar with returning. This article summarizes how to solve the return value problem gracefully.

Test program

We usually get the output of the last statement through $? Look at the following test statements:

New testReturn script

 return $1
returnString $1
echo "result=$result"

Now we have a testReturn script with a return String method, which we hope will return directly the parameters we entered.
When we take hello, 500, 12 as input parameters, are their execution and output the same?

./testReturn hello
./testReturn 500
./testReturn 12

In mind, try to guess the possible situation. Now let’s reveal the answer:

Program Output

When Hello is executed, instead of outputting hello, an error is reported that returns only accept numeric types. aven$ ./testReturn hello
./testReturn: line 23: return: hello: numeric argument required

When 500 is executed, the page does not output 500, but 244. aven$ ./testReturn 500

At the time of execution 12, finally correct, return 12 aven$ ./testReturn 12

Anomaly analysis

Now let’s analyze the return String method. Why are there so many output cases?

First of all, his writing is obviously not rigorous, but it is not entirely wrong, such as typing 12, he returned correctly.

Return itself is the buildin function in the shell. The author summarizes the following characteristics:

  • Return can return a numeric state, often used to return 0, 1, indicating whether a function has been successfully executed
  • Note that return cannot return non-numeric types
  • At the same time, digital types may also overflow.

global variable

What if we just want to return a string? We can assign values by defining global variables, similar to static variables / member variables, so that their scope can penetrate the whole context.

returnString $1
echo "result=$result"

Looking at the output again, we get the results we need: aven$ ./testReturn hello
result=hello aven$ ./testReturn 500
result=500 aven$ ./testReturn 12

However, writing this way will pollute the global variables, and the result variable can easily be modified both internally and externally, leading to the failure of internal modifications.


In addition to return, there are other keywords for buildin, such as eval, local.

By default, the variables defined in the current script are global variables, while in the method, local variables can be defined to avoid global variable pollution.
At the same time, it combines Eval assignment statement to modify variables.

 local __result=$2
 eval $__result=$1
returnString $1 result
echo "result=$result"

Similarly, we have achieved the desired results. aven$ ./testReturn hello
result=hello aven$ ./testReturn 500
result=500 aven$ ./testReturn 12


Finally, a method is introduced, which combines command substitution with echo output.

The command substitution also failed to find a suitable translation, let’s translate the command literally.

If you have only one echo output inside your method, you can also use it to make it worthwhile to return, but this has some limitations. Make sure that there is only one echo output in your method, otherwise there will be too much assignment.

 local __result=$1
 echo $__result
# Or result=`return String $1`
result=$(returnString $1)
echo "result=$result"

Expected results can also be obtained. aven$ ./testReturn hello
result=hello aven$ ./testReturn 500
result=500 aven$ ./testReturn 12

Cross-border issues

Now that we have several ways to return strings, why are returned numbers sometimes correct and sometimes incorrect?

We know that return is originally used to return execution status, such as 0, 1. So when we return 500, we actually overflow data.

According to the test, we infer that the shell’s built-in return acceptance return value uses a byte size, that is, 8 bits, which can output an unsigned 0-255 shaping at most, and all the data outside the scope is overflowed. So when using return, be sure to pay attention to the value size.


It’s easy to write small scripts with shell commands, but if you encounter complex logic, it’s advisable to use other more appropriate previews, such as Python, Golang and so on.


Above is the whole content of this article. I hope that the content of this article has a certain reference value for everyone’s study or work. If you have any questions, you can leave a message and exchange it. Thank you for your support to developpaer.

Recommended Today

Manjaro uses SS method (requires nodejs environment)

Just installed manjaro, need to installshadowsocks-qt5+Proxy SwitchyOmega, but the latest Chrome has long blocked the installation of non Google store CRX channels. Here is my solution, which is troublesome but usable. If you are familiar with NPM commands in nodejs, you won’t find it troublesome, because nodejs is used for compilation. Preparation environment: A VPS […]