Shell Builtin Command 「return」
type return
執行
$ type return
顯示
return is a shell builtin
help return
執行
$ help return
顯示
return: return [n]
Return from a shell function.
Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the
last command executed within the function or script.
Exit Status:
Returns N, or failure if the shell is not executing a function or script.
Exit Status
interactive
執行
$ return
顯示
bash: return: can only `return' from a function or sourced script
再執行
$ echo $?
顯示
1
script return
script_return.sh
#!/usr/bin/env bash
return 3;
設為可執行
$ chmod u+x ./script_return.sh
執行
$ ./script_return.sh
顯示
./script_return.sh: line 3: return: can only `return' from a function or sourced script
再執行
$ echo $?
顯示
1
source return
source_return.sh
return 3;
call_source.sh
#!/usr/bin/env bash
source source_return.sh
# . source_return.sh
echo $?
「call_source.sh」設為可執行
$ chmod u+x ./call_source.sh
執行
$ ./call_source.sh
顯示
3
再執行
$ echo $?
顯示
0
function return
function_return.sh
#!/usr/bin/env bash
function_return () {
return 3;
}
function_return
echo $?
設為可執行
$ chmod u+x ./function_return.sh
執行
$ ./function_return.sh
顯示
3
再執行
$ echo $?
顯示
0