The batch script closes once it's done because that's how batch scripts function. Once a script has no more code to run, the window closes. If you want to keep the window open, you can use the pause
command or run the script from the command prompt instead of double-clicking it.
You are missing a =
at the end of the set /p
command, which is what is causing the "the syntax of the command is incorrect" error.
It is generally considered a good idea to use quotes, brackets, or some other character when comparing strings, just in case the user hit enter
without typing anything. Like Serenity mentioned, if you have an option to use the choice
command, use that, since it's more robust.
It's also considered good practice to have ""
immediately after a start
command, just in case you need to start something that needs to be wrapped in quotes; start
considers the first thing in quotes it encounters to be the window title and then everything after that is the command. As you can imagine, this has the potential to break your code.
And so, in summary:
@echo off
title Search Providers
cls
:home
echo [1] Google
echo [2] Bing
echo [3] Yahoo
set /p udefine=
:: Ideally there's some sort of error handling here as well
if "%udefine%"=="1" start "" www.google.com
if "%udefine%"=="2" start "" www.bing.com
if "%udefine%"=="3" start "" www.yahoo.com
pause