Return value error in bash scripts

5,614

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.

Share:
5,614

Related videos on Youtube

gs rao
Author by

gs rao

Updated on September 18, 2022

Comments

  • gs rao
    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
      Yaron about 6 years
      what is the next step? what exactly does the next step check?
    • gs rao
      gs rao about 6 years
      The 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
    gs rao about 6 years
    I 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
    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
    gs rao about 6 years
    Oh..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
    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 of rmdir, 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
    gs rao about 6 years
    My 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
    Kusalananda about 6 years
    @gsrao Other shells, such as ksh and dash allows for return from a shell script. You may have used another shell before. It's also possible that older versions of bash allowed this, but I don't have an older version of bash to test this with.
  • gs rao
    gs rao about 6 years
    I 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
    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 that sh should not be used to run bash scripts (you tagged your question with bash).
  • gs rao
    gs rao about 6 years
    I 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
    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.