In the script below, when the code inside the if statement is executed, the user gets logged out from bash. Why is this?
#!/bin/sh
if [ -z $1 ]; then
echo "1"
read w
exit 1
fi
if [ "$#" -gt "1" ]; then
echo "1"
read w
exit 2
fi
export PTSUSER=$1.
<some more code>
If the script is sourced, and either of the
ifblocks are hit, then yes the current users bash shell will be exited, and the user will be logged out. This is due to sourcing a script will use the current users bash shell to execute the script.On the other hand if it’s run in a new bash shell instance, then this won’t happen, and the bash instance will exit once either of the exit commands are executed in the shell (in either of the if blocks).