0

I have the latest OS X running the standard terminal app, with bash 4.0 via homebrew

However, if I run a script that contains an exit command, it's expected it would exit the script. Instead the session ends and my tab vanishes.

I've changed my settings to keep the tab open, but my session still ends, and as I understand this, it's non-standard behaviour.

What can cause this to happen?

As a sidenote, here is my bash prompt etc

2
  • Do you run the script with . script or source script? Aug 29, 2014 at 14:19
  • I run it with . script Aug 29, 2014 at 14:21

2 Answers 2

2

I run it with . script

That means you execute the script in your current shell, so the exit kills your current shell. This is expected behaviour.

You should probably either

  1. give the script execute permissions (chmod u+x script) and run ./script, or
  2. invoke it with bash script to run it in a new shell process.
0

This is my script:

cat > doExitKeepShell.sh << EOF
#!/bin/bash
sleep 5
exit 0
EOF

and its executable:

chmod u+x doExitKeepShell.sh

Run it so and tab will get closed

$: . doExitKeepShell.sh
# wait 5 sec and window get closed

Run it so and wait 5 sec to proceed with shell

$: ./doExitKeepShell.sh 
# wait 5 sec till prompt return ;-)

Run it so and keep on working

$: . doExitKeepShell.sh &
[1] PID    # <- this is your script in background
# Keep on working

If you want to go to script type

$: %

If exec of script ended, you will see s.th. like that

$: %
-bash: fg: job has terminated
[1]+  Done                    ./doExitKeepShell.sh

If you have several background jobs you can type

$: % PID

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .