#! /bin/ksh
#first.sh
echo "b4 set exit as return"
alias exit=return
echo "call second"
. ./second.sh
echo "after second"
#. ./third.sh
unalias exit
echo "ho lanciato il terzo"
=================//
#second.sh
echo "in scond"
exit
==============//
the above code works in k shell, but not works in bash, how to write the same in bash..
Your exit
in second.sh causes first.sh to terminate because it's all the same shell. Remove that and it would run fine.
I don't think that the alias exit=return
will be effective, but it is very confusing nonetheless.
I get this output when run with the trace option:-
$ bash -x first.sh
+ echo 'b4 set exit as return'
b4 set exit as return
+ alias exit=return
+ echo 'call second'
call second
+ . ./second.sh
++ echo 'in scond'
in scond
++ exit
Oddly, it seems to work in ksh:-
$ ksh -x first.sh
+ echo 'b4 set exit as return'
b4 set exit as return
+ alias exit=return
+ echo 'call second'
call second
+ . ./second.sh
+ echo 'in scond'
in scond
+ return
+ echo 'after second'
after second
I hope that this helps,
Robin
bash by default does not expand aliases in non-interactive shells..
Try something like this:
shopt -s expand_aliases 2>/dev/null
echo "b4 set exit as return"
alias exit=return
Then it should run in either shell