1

I'm trying to write a simple script (for an embedded system) that mounts an external file from the network and then calls exit on the command line (which exits the busybox terminal and boots the system from the newly-mounted root directory). Problem is, when I call exit from the shell script it exits the script, not the outer terminal. Any hints as to how to send the exit command outside the script?

My code looks something like this:

#!/bin/bash
mount /mnt/root 1.2.3.4:/path/to/external/files -o nolock
exit   # exits the script, not the outside filesystem!
chessbot
  • 436
  • 2
  • 11
  • well, your parent shell is(`outer shell`) calling a script which gets executed in a child process, a subshell. No way you can call something which gets executed in parent subshell. But instead of calling the script as `./somescript.sh`, you can call it as `./somescript.sh && exit` – abasu Jun 03 '13 at 15:40

2 Answers2

3

exit does indeed exit the current shell. However, running the script creates a new process, and it is that process that exits, not the shell from which you called the script.

You probably want to simply source the file:

$ source myScript.sh

Then myScript.sh is executed in the current shell, rather than a spawned process, and the exit will exit from your current shell (and therefore the terminal itself).

chepner
  • 497,756
  • 71
  • 530
  • 681
1

If you want that the scripts exits the outer terminal call it like this:

source your_script

or just

. your_script

where the . is the same as the source command. You can follow this article if you need more information.


Explanation: source will execute the script in the current shell and therefore the exit will be interpreted by the current shell - what will close the pseudo(!) terminal window, if the shell was the first shell in tree

hek2mgl
  • 152,036
  • 28
  • 249
  • 266