Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent terminal window from closing when Linux Bash Script terminates?

Tags:

linux

bash

I've written a simple bash script for deleting some temporary files that match a certain pattern:

#!/bin/bash
cd ~/some_project/some_dir
rm */*.xml

I've saved it as 'script', chmodded it to +x via terminal. Now when I double click this file, I get the option to Run it in terminal or display its output. If I click Run in terminal, the script correctly works, however the terminal window closes immediately. I'd rather the window stay open so I can see if there were any errors (and possibly get rm to display names of deleted files if possible).

I'm looking for a simple way to keep the terminal from closing until a keypress happens.. is there such a way?

like image 208
Ali Avatar asked Jan 21 '13 20:01

Ali


2 Answers

Add read command to the end of your script. It will wait for a complete line of input (that is, Enter key).

like image 75
Anton Kovalenko Avatar answered Sep 21 '22 17:09

Anton Kovalenko


read -rn1

See help read for more information.

like image 27
melpomene Avatar answered Sep 17 '22 17:09

melpomene