Oh, my girlfriend really likes it when I launch php scripts! There's something special about them, she says ...

Thus, I coded this script to run throught the CLI (Command Line Interface) - so it's running locally, not on a web server.

It launches just fine through right click > open > run in terminal but closes right after execution. **Is there a way to keep the terminal window open?

Of course I can launch it through a terminal window - which would stay open - but I'm looking for a one click action. With bash scripts I use $SHELL but that didn't work (see code below).

So far, the only thing I came up with is sleep(10); which gives me 10 seconds for my girl to check the output. I'd rather close the terminal window manually, though.

#!/usr/bin/php -q

    echo "Hello World \n";

    # wait before closing terminal window

    # the following line doesn't work


(PHP 5.4.6-1ubuntu1.2 (cli) (built: Mar 11 2013 14:57:54) Copyright (c) 1997-2012 The PHP Group Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies )

  • possible duplicate of How to run a script without closing the terminal?
    – Panther
    Mar 29, 2013 at 17:34
  • 1
    The question is not a duplicate. The link provides a solution for bash scripts. In the above case it is a php script ... adding $SHELL doesn't work.
    – somethis
    Mar 31, 2013 at 10:45
  • 2
    You are mixing bash and php code. The correct way would be system("\$SHELL");.
    – Salem
    Mar 31, 2013 at 12:49

1 Answer 1


As Salem said in this comment, you are mixing bash and php code. You can use shell_exec php function to prevent this:

#!/usr/bin/php -q

    echo "Hello World \n";

    # prevent to close terminal using php


string shell_exec ( string $cmd ) — Execute command via shell and return the complete output as a string.


  • Thanks for leading this to an officially answered question :)
    – somethis
    Jul 1, 2013 at 21:59

You must log in to answer this question.

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