3 added 336 characters in body
source | link

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.


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.

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.

2 added 419 characters in body
source | link

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.


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.

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.


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.

1
source | link

The return statement returns an error if the statement is not executed from a function.

In shell functions, 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.