Search code examples
bashbackgroundampersand

Can I run two bash commands at once and send only one to the background?


In the interest of avoiding an XY problem, I'm trying to set up an iTerm profile to do the following when I hit the hotkey.

  1. cd to ~/my/directory/
  2. start an http server in the background
  3. let me stay in ~/my/directory/

The command I'm working with so far is cd ~/my/directory/ && python -m SimpleHTTPServer 8000 &> /dev/null &

This works great for starting the server on the project root. The problem is, after it starts running in the background, my pwd is NOT ~/my/directory/. I guess that part is sent to the background too.

Is there a way to accomplish step 3 without having to cd again manually afterward?


Solution

  • This works for me

    ; instead of &&

    cd ~/my/directory/ ; python -m SimpleHTTPServer 8000 &> /dev/null &
    

    or second command in ( )

    cd ~/my/directory/ && (python -m SimpleHTTPServer 8000 &> /dev/null &)