bash function executes php but then pauses / bash function gets stuck

Viewed 75

I am using WSL (windows subsystem linux) to locally develop PHP files.

I am using the inbuilt PHP server to run them via Ubuntu:

php -S localhost:8000

I've made a Bashrc function to do this for me but it pauses after creating the localhost.

phpLocal(){
        php -S localhost:"$1"
        cmd.exe /C start http://localhost:"$1"
}

The goal is to run

phpLocal 8200

and this to open a web browser with the local host running localhost:8200.

The issue

Bash accepts the command - i.e. creates the localhost server on port 8200, but the browsers does not open it.

enter image description here

Then when I cancel the command the browser does open it, but because the command has been canceled there is nothing to open..

enter image description here

I suspect I am missing some in how bash runs functions.

Any help would be great - Wally

-- Requested information

I am calling this function like this

phpLocal 8200

-- renamed question as I believe it not to be a duplicate, but the answer to the issue to be the same. I think its valuable for people searching broader terms relating to bash functions...

1 Answers

When you execute this function:

phpLocal(){
        php -S localhost:"$1"
        cmd.exe /C start http://localhost:"$1"
}

... the php command executes in the foreground. If you want it to continue executing so that the cmd.exe line can execute, you could put the php line in the background with the & symbol:

phpLocal(){
        php -S localhost:"$1" &
        cmd.exe /C start http://localhost:"$1"
}
Related