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

Mar 31, 2016 11:47 AM
Apr 1, 2016 05:34 AM

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".

635949934014955665.jpg

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

635949932524957972.jpg

to this:

635949933161674561.jpg

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:

635949937095580401.jpg

to this:

635949937886518277.jpg

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.

635949951411205847.jpg

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 :)

635949956623082427.jpg

Step 6:

Now we need to save it.

Save your file by clicking Save. You will see this window.

635949959260893249.jpg

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:

635949960244486750.jpg

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:

635949962013549936.jpg

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

635949962314330764.jpg

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!

Comments

No Comments Exist

Be the first, drop a comment!