1、区别:
| 主shell 中 | 函数中 |
---|
exit | 退出主shell | 退出主shell |
return | 退出主shell | 退出函数 |
2、测试实例:
[/etl/shell]$cat test.sh
echo "111"
ls aaaa
return
echo "222"
[/etl/shell]$
[/etl/shell]$sh -vx test.sh
echo "111"
+ echo 111
111
ls aaaa
+ ls aaaa
aaaa not found
return
+ return
[/etl/shell]$
[/etl/shell]$vi test.sh
[/etl/shell]$cat test.sh
echo "111"
ls aaaa
exit
echo "222"
[/etl/shell]$sh -vx test.sh
echo "111"
+ echo 111
111
ls aaaa
+ ls aaaa
aaaa not found
exit
+ exit
[/etl/shell]$
[/etl/shell]$cat test2.sh
ftest() {
echo "111"
ls aaaa
return
}
ftest
echo '$?='$?
echo "222"
[/etl/shell]$
[/etl/shell]$sh -vx test2.sh
ftest() {
echo "111"
ls aaaa
return
}
ftest
+ ftest
111
aaaa not found
echo '$?='$?
+ echo $?=2
$?=2
echo "222"
+ echo 222
222
[/etl/shell]$
[/etl/shell]$vi test2.sh
[/etl/shell]$cat test2.sh
ftest() {
echo "111"
ls aaaa
exit
}
ftest
echo '$?='$?
echo "222"
[/etl/shell]$sh -vx test2.sh
ftest() {
echo "111"
ls aaaa
exit
}
ftest
+ ftest
111
aaaa not found