Understanding Command Line Arguments and How to Use Them

  • September 8, 2017
  • Read 210,017 times
 

What are Command Line Arguments and why would you use them?

Command line arguments are extra commands you can use when launching a  program so that the program's functionality will change.  Depending on the program, these arguments can be used to add more features that includes specifying a file that output should be logged to, specifying a default document to launch, or to enable features that may be a bit buggy for normal use.

In order to understand what a command line argument is, we should show an example of how a program is normally launched. In Windows, when you start a program by clicking on it's icon, or shortcut, it simply runs an executable and the program runs with whatever default settings are programmed into it. For example, the C:\Windows\system32\notepad.exe program is the Windows Notepad. To launch it, you would simply type notepad into the search field and press enter or click on its icon.  All this does is start the Notepad.exe program as shown by the Target field in the shortcut properties below. Note, in the shortcut Target field below, %windir% means the folder Windows is installed into, which is usually C:\Windows on most PCs.

Notepad Shortcut

A command line argument is simply anything we enter after the executable name, which in the above example is notepad.exe. So for example, if we launched Notepad using the command C:\Windows\System32\notepad.exe /s, then /s would be the command line argument we used.

It is important to note that you must add a space between the program you want to run and the command line argument. For example, notepad.exe/s is not a valid argument because it does not contain a space. Instead you must add a space so it looks like notepad.exe /s.

How to use Command Line Arguments

Program's arguments are named command line arguments because they are typically used when you type them within a console, command prompt, or terminal. In a graphical user interface, when you want to move a file from one folder to another, you just drag it. On the other hand if you want to move a file from a console command, you need to specify the file and location using command line arguments, which are typed after the program name.

For example, in a Windows Command Prompt, to execute a program you must type its name and press Enter on the keyboard. These programs typically take arguments in order to work properly and these arguments are called command line arguments. Let's use the move program as an example.This command requires two command line arguments; the file to move and where you wish to move it. Therefore a valid use of the move program would be move test.txt c:\, which would move the test.txt file to the root of the C:\ drive as shown below.

Move Example

This same behavior works in Terminal on macOS and Linux as well as any other console environment. You type in the name of the program, followed by its arguments that tell the program how it should operate.

Now, just because they are called command line arguments does not mean they can only be used from a console.  For example, in Windows you can modify a shortcut so that it permanently uses a particular argument in order to perform a certain behavior every time you launch the program from the icon.

Let's use the above Notepad shortcut as an example. In the shortcut properties, the program that will be executed is listed in the Target field.  Now let's say that we wanted to have Notepad automatically open a particular file every time it launches. To do this, we would add the name of the file to be opened as an argument to Notepad. We do this by adding the document name, which in this case is C:\mynotes.txt, after notepad.exe in the Target field as shown below.

Now every time you launch Notepad, it will automatically open the mynotes.txt file.

Why would you use command line arguments?

Most people will use their computer and never use a command line argument at all. On the other hand, power users, programmers, and IT professionals may use them on a daily basis. For example, when I use the computer I always have a Windows Command Prompt open simply because I find it faster and easier to type in certain commands than to do it via the Windows graphical user interface.

Command Line Arguments are also extremely useful when used in batch files and login scripts as they allow you to create very complex tasks through command line programs.

WIth that said, command line arguments are not only used in a console. You can use them directly within Windows by just entering the command into the search field followed by the argument. I use notepad a lot, so I will use it for another example. I tend to have a lot of things going on at one time, so I keep notes in various text files in a particular folder on my computer. If I wanted to open a particular text file, I would just type notepad d:\notes\todo.txt in the Start Menu's search field, press Enter on the keyboard, and my text file opens. On the other hand, performing this task via the standard user interface of Windows would take longer for me.

Open a text file

As you can see there are many reasons why you would use command line arguments and each person will use them differently. As you start using them and getting familiar with them, you will start to use them more and more.

How to find a programs available Command Line Arguments

Now that you know how to use a command line argument, you may be wondering how you can get a list of arguments that are available in a program. Usually you can find out a programs arguments by typing either /?, -h, -?, or --help after a command, which will hopefully print out a programs available command line arguments. Unfortunately, there is no set method that tells a program to list its arguments and different developers will display them differently.

In my experience, though, I have found that the /? command works on all Microsoft programs that are shipped with Windows. Simply type the command followed by /? and the program should display its help file. This can be seen in the example below where we see the command line arguments for the Windows del command.

In Linux and macOS, the programs shipped with the OS will typically use a --help argument to show the help file as can be seen below.

Linux Command Line

For other programs that you download from the Internet, figuring out the command line arguments are going to be more difficult. I usually start with the /?, -h, -?, or --help arguments to see if it will cause the program to spit out a help file. If it does not, I then resort to either reading a manual or searching on Google.

Users who read this also read:

  • How to Open and Use the Google Chrome Task Manager Image
    How to Open and Use the Google Chrome Task Manager

    The Google Chrome browser has a built-in Task Manager that allows you to see how much memory and CPU web pages, extensions, and Google processes are using while Chrome is running. This feature is very useful if you find that Chrome has become slow, is using too much memory, or freezes a lot as you can see those extensions or pages are causing a problem.

  • How to determine why Google Chrome is using a lot of memory or CPU Image
    How to determine why Google Chrome is using a lot of memory or CPU

    Unfortunately, it has become very common to find malicious Chrome extensions, poorly designed extensions, or extension monetization that causes Google Chrome to use up too much CPU or memory on a computer. This can cause the Chrome and the Windows to become slow, freeze, or crash. 

  • Introduction to the Windows Command Prompt Image
    Introduction to the Windows Command Prompt

    Before Windows was created, the most common operating system that ran on IBM PC compatibles was DOS. DOS stands for Disk Operating System and was what you would use if you had started your computer much like you do today with Windows. The difference was that DOS was not a graphical operating system but rather purely textual. That meant in order to run programs or manipulate the operating system ...

  • How to shut down or restart Windows Image
    How to shut down or restart Windows

    It is important to know how to properly shut down or restart your computer so that you do not lose data or corrupt important Windows files or Registry locations. Many people think that you shut down your computer simply by pressing the power button. On some configurations, this will work as Windows will recognize that you press the power button and shut it down gracefully. On the other hand, if ...

  • How to find the command line arguments a program started with in Windows Image
    How to find the command line arguments a program started with in Windows

    Some programs provide the ability to add arguments when executing it in order to change a particular behavior or modify how the program operates. As an example lets look at the command line argument for Firefox called safe-mode. If you start Firefox with the command line firefox.exe -safe-mode Firefox will start without any extensions or themes. As you can see adding a command line argument to the ...

 

Comments:

blog comments powered by Disqus
search tutorials
Mandiant mWise Conference 2024

Login