How to run multiple shell commands one after another in Windows batch script?

Welcome to Programming Tutorial official website. Today - we are going to cover how to solve / find the solution of this error How to run multiple shell commands one after another in Windows batch script? on this date .

I tried using '&' as most of similar posts suggest but this doesn’t work:

@echo off
call variables.bat // contains port numbers and notebook address
ssh user@remote_server "jupyter notebook --no-browser --port=%port_r%" & 
ssh -N -f -L localhost:%port_l%:localhost:%port_r% user@remote_server &
start chrome %notebook_address% 
@PAUSE

I basically have two shell scripts that allow me to run jupyter notebook remotely and connect to it. I run them manually one after another and I want to combine them in a single script.

The first one runs jupyter notebook on remote server:

@echo off 
call variables.bat  // contains port numbers and notebook address
ssh user@remote_server "jupyter notebook --no-browser --port=%port_r%" 
@PAUSE

Second one provides port forwarding:

@echo off
call variables.bat
ssh -N -f -L localhost:%port_l%:localhost:%port_r% user@remote_server
@PAUSE

How could I combine the two?

Answer

The Windows command-line shell is not Bash – it’s Cmd.exe, sometimes called “batch”.

If you want to run both commands simultaneously, then it would indeed be & in Bash, but in Cmd that doesn’t have the “background” effect; it only does the exact same thing as just putting the two commands one after another in separate lines.

To run something in parallel, you can most likely use start here as well:

@echo off
call variables.bat
start ssh user@remote_server "jupyter notebook --no-browser --port=%port_r%"
start ssh -N -f -L localhost:%port_l%:localhost:%port_r% user@remote_server
start chrome %notebook_address% 
pause

Though regarding the two ssh connections, I don’t see why they couldn’t be combined into one (i.e. just invoking the actual command and setting up the forwarding at the same time, instead of using -N):

@echo off
call variables.bat
start ssh -f -L localhost:%port_l%:localhost:%port_r% user@remote_server "jupyter notebook --no-browser --port=%port_r%"
start chrome %notebook_address% 
pause