Return value error in bash scripts
The return
statement returns an error if the statement is not executed from a function or a dot-script (a sourced script).
In shell functions and dot-scripts, use return
. In scripts, use exit
.
A short script like
#!/bin/bash
return 0
will produce the error message
line 1: return: can only `return' from a function or sourced script
and it will set $?
to 1.
If you don't exit the script with an explicit exit
(or if you exit with exit
but without specifying a exit code), the return code of the script as a whole will be the same as the last executed command.
Having exit "$?"
is the same as plain exit
, and if it's at the end of the script, this can be left out completely.
In general, I also suggest that you use $HOME
rather than tilde in scripts. This is because $HOME
works like any other variable, whereas tilde is expanded in a separate expansion step, which means that it does not behave as a variable and that it is not expanded in quoted strings. $HOME
is also more descriptive and since it's a script, you can spend few extra keystrokes to make the code more readable.
Related videos on Youtube
gs rao
Updated on September 18, 2022Comments
-
gs rao over 1 year
I have bash script like this:
rm ~/sqoop/"$TABLE"/* rmdir ~/sqoop/"$TABLE" return $?
After execution this script it returns a value to next process but even though the script run successfully and executing all stages in script completed successfully it is returning 1.
Because this - the next process is not taking place.
If I remove the return statement the next process going smoothly. this problem came after the Hadoop cluster up gradation which having Ubuntu 14 and new cluster have Ubuntu 16.
Can you please help us to understand what is issue and how the next process is running successfully if remove the return command here and what is impact in production if remove the Return statement?
-
Yaron about 6 yearswhat is the next step? what exactly does the next step check?
-
gs rao about 6 yearsThe next is HQL(hive query in Hadoop) and it has some business steps it is running if i comment the return value it is working good but not running if comment the return. i have't changes anything on business logic/shell script the only thing we had done is upgrade
-
-
gs rao about 6 yearsI have quick query for above your response..i will try above with exist 0 but i have one query why this issue happens now only previsouly i use same query ? it is working if i comment return statement is this impact any thing?
-
Kusalananda about 6 years@gsrao If you comment out the
return
statement, the return code of the script will be that of the last executed command, so in your case here, you can definitely leave it out. I will add this to my answer. -
gs rao about 6 yearsOh..great ..you are saving my life, so my case if i comment the return by default script will return last comment successful flag(rmdir ~/sqoop/"$TABLE") right? ..here even though your answered work me if two simple queries which are eating my brain 1) my case what is use of "return $?" like you said it will work like same if i comment the "return $" 2) what may be possible causing the issue earlier to now?
-
Kusalananda about 6 years@gsrao Yes, if you comment out the
return
(which shouldn't be there to start with since it's not in a function, as far as I can see), the exit status of the script will be that ofrmdir
, if that's the last command in the script. As for your last question, I don't quite understand what you mean by "the issue earlier to now". -
gs rao about 6 yearsMy last question..i am using same script what i have use before linux/cluster up gradation it was working until cluster up gradation, why now only this issue happens? what might be the cause this issue?
-
Kusalananda about 6 years@gsrao Other shells, such as
ksh
anddash
allows forreturn
from a shell script. You may have used another shell before. It's also possible that older versions ofbash
allowed this, but I don't have an older version ofbash
to test this with. -
gs rao about 6 yearsI have testing this script in ubuntu 14 using "sh script_name.sh" it is working but i use same script in ubuntu 16 using " sh script_name.sh" it throwing error "return: can only `return' from a function or sourced script"..so it this is upgrade is root cause of this issue?
-
Kusalananda about 6 years@gsrao I would respectfully say that the erroneous use of
return
in the script is the root cause, and that the upgrade highlighted this. Also note thatsh
should not be used to runbash
scripts (you tagged your question withbash
). -
gs rao about 6 yearsI agree with your point but if i run script using "bash" in 14 also giving same error ..it is working only if i run the script with sh only..
-
Kusalananda about 6 years@gsrao As I said previously, different shells handle this differently. An up to date bash will complain. I don't know what shells masquerade as sh on the two versions of Ubuntu that you have been using. If you want to run it under a current bash, then don't use return other than from a function.