Batch Basics: Understanding the Windows Command Line, Part 1 (Echo and Variables)

Understanding the Windows Command Line, Part 1 (Echo and Variables)

Welcome back, everyone!

In this lesson, I'm going to go over the use of the echo function and variables, as well as basic syntax.

You will need:

  • A text editor - I personally recommend the free (and amazing) Notepad ++, but you can use any text editor you want, even Windows' built-in Notepad.

I will be assuming you are using Windows for this series, but you can use any Linux distro that you want, you will just need the Windows command prompt to run these scripts.

Step 1:

This is pretty simple, just launch your text editor. If you are using Notepad, then press Windows button and R to open the Run dialog, then type "notepad" into the search box.

If you are using Notepad ++, once it is opened and you have started a new document, click on the Languages tab and select Batch from the drop-down menu.

Step 2:

At the start of the file, type "@echo off".

What this does is that it gets rid of the user prompt in CMD (Command Prompt), changing it from this:

to this:

It seems like a small change, but it can be important to have.

Step 3:

Then, type in "title Null Byte Batch Script" or whatever you want the command prompt window to be titled.
This changes the text on the CMD screen from this:

to this:

Next comes the 'echo' command.

This command writes text to the screen. For example, if you type in "echo hello", the command prompt will return 'hello'.

One of the nice things about batch is it's simplicity. You rarely need brackets or quotes, you can just type in the commands without worrying about formatting.

So now, under the Title command, we will type "echo What is your name?"

This will be our question to the user. For the answer, we need a variable.

Step 4:

Variables are declared by enclosing the variable in percentage signs (%). Bear in mind that variables are case sensitive, so %variable% is different to %VARIABLE% is different to %Variable%.

To define the value of a variable ourselves, we use "set /a VARIABLENAME= VARIABLEVALUE"

However we want the user to set the variable (by answering the question), so we will replace /a with /p.

So now, enter "set /p useranswer=" into your text editor.
When we are setting a variable, we do not need to enclose it in %'s.

This will display a blank line on the command prompt, allowing the user to enter their name.

Step 5:

Now we want to display the user's name. To do this, we will type:
"echo Hello, %useranswer%!"
Unlike many other languages, you do not need to use the & symbol to insert a variable into a string.

Now, underneath that, type "pause >nullbyte".

The 'pause' command pauses the command prompt, waiting for the user to press a key. Without it, the command prompt would close directly after you entered your name.

The '>nullbyte' tells the 'pause' command to write it's contents to a file called 'nullbyte' You can name this file anything you want, and if the file doesn't exist, Windows will create it for you. Without the '>nullbyte', the command prompt would print "Press any key to continue...", but it looks kind of ugly, so we can tell it to write to a file instead :)

Step 6:

Now we need to save it.
Save your file by clicking Save. You will see this window.

If the 'Save as Type window says 'Batch File' or '.bat', then just enter the name of your batch file and click Save!

If, however, you get this screen:

Make sure that the file type is set to 'All types' if the 'Batch' option isn't available, and make sure that it ends in '.bat'.

Step 7:

Time to run it!
Just open Explorer and double-click on your batch file.
You should see a screen similar to this:

Type in your name and you should get something like this:

If you got that, congratulations! If you didn't, go back over my tutorial and make sure you followed each step precisely (and without the quotation marks) :)

Stay tuned!

Just updated your iPhone? You'll find new features for Podcasts, News, Books, and TV, as well as important security improvements and fresh wallpapers. Find out what's new and changed on your iPhone with the iOS 17.5 update.


Can't we add a "del nullbyte" after the pause command to delete the file after user pressed a key to prevent the nullbyte file from remaining?

You could, but there isnt really much point, as it takes up >1kB of space, and if you have multiple batch files, you can make them use the same file.

I suggest you crop your images before pasting them into the article so people can read it properly without having to manually view the image in full screen or squint their eyes.

Also, just a note for the `pause` command, you can direct it to nul which discards the standard output or error.

Yes, sorry about the images, I didnt realise they would be that bad :)

I didn't know about the nul command, thankyou! Ill keep it as a file though, because ill be covering read/write files later, which use the same syntax.

Thanks for this series! this is very usefull. I'll be following it :)

but you explained very well.
i am impressed. but plzz always explain us with this same style...
thanks for this its very helpful.

Share Your Thoughts

  • Hot
  • Latest