batch-fileDifferences between Batch (Windows) and Terminal (Linux)

Introduction

Batch and bash are quite different. Batch flags are indicated with a /, while bash flags use a -. Capitalization matters in bash, but (almost) not at all in batch. Batch variable names can contain spaces, bash variable names can not. Ultimately, both are ways of manipulating and interacting with the command line. Not surprisingly, there is a reasonably-sized amount of overlap between the capabilities of the two systems.

Remarks

  • bitsadmin is deprecated in favor of the PowerShell cmdlet BITS but still works as of Windows 10 version 1607
  • certutil separates pairs of hexadecimal digits with a space, so md5sum will return an example value of d41d8cd98f00b204e9800998ecf8427e, while certutil displays the same value as d4 1d 8c d9 8f 00 b2 04 e9 80 09 98 ec f8 42 7e
  • To cd to another drive (for example, from C: to D:) the /d flag must be used
  • del can not delete folders, use rm instead
  • grep is so much more powerful than find and findstr, it's almost not fair to compare them; find has no regex capabilities and findstr has extremely limited regex capabilities ([a-z]{2} is not valid syntax, but [a-z][a-z] is)
  • for loops on the Windows command prompt can only use single-character variable names; this is the only time batch variable names are case-sensitive
  • for loops on the command prompt also use the variable form %A instead of %A% - forloops in batch scripts use the variable form %%A
  • md automatically creates any necessary parent directories, while mkdir needs the -p flag to do so
  • rem may not be used as an inline comment character unless it is preceded by a &
  • :: may not be used as an inline comment at all, and should also not be used inside of a code block (set of parentheses)
  • Note that some Windows command like ping still uses - as flags

Batch Commands and Their Bash Equivalents

BatchBashDescription
command /?man commandShows the help for command
bitsadminwget or curlDownloads a remote file
certutil -hashfile file_name MD5md5sum file_nameGets the MD5 checksum of file_name
cdpwdDisplays the current directory
cd directorycd directoryChanges the current directory to the specified one
clsclearClears the screen
copycpCopies a file or files from a source path to a target path
datedateDisplays the date or sets it based on user input
delrmDeletes a file or files
dirlsdisplays a list of files and directories in the current directory
echoechoDisplays text on the screen
exitreturnExits a script or subroutine
exitlogoutCloses the command prompt or terminal
fcdiffCompares the contents of two files
find "string" file_namegrep "string" file_nameSearches file_name for string
findstr "string" file_namegrep "string" file_nameSearches file_name for string
for /F %A in (fileset*) do somethingfor item in fileset*; do; something; doneDo something for every file in a set of files
for /F %A in ('command') do something`command`Returns the output of a command
for /L %A in (first,increment,last) do somethingfor item in `seq first increment last`; do; something; doneStarts at first and counts by increment until it reaches last
forfilesfindSearches for files that match a certain criteria
if "%variable%"=="value" (if [ "variable"="value" ]; thenCompares two values
ipconfigifconfigDisplays IP information
mdmkdirCreates new folders
mklinkln -sCreates a symbolic link
moremoreDisplays one screen of output at a time
movemvMoves a file or files from a source path to a target path
pauseread -p "Press any key to continue"Pauses script execution until the user presses a button
popdpopdRemoves the top entry from the directory stack and goes to the new top directory
pushdpushdAdds the current directory to the directory stack and goes to the new top directory
renmvRenames files
rem or ::#Comments a line of code
rdrmdirRemoves empty directories
rd /srm -rfRemoves directories regardlesss of whether or not they were empty
set variable=valuevariable=valueSets the value of variable to value
set /a variable=equationvariable=$((equation))Performs math (batch can only use 32-bit integers)
set /p variable=promptstringread -p "promptstring" variableGets user input and stores it in variable
shiftshiftShifts arguments by 1 (or n if provided)
sortsortSorts output alphabetically by line
tasklistpsShows a list of running processes
taskkill /PID processidkill processidKills the process with PID processid
time /tdateDisplays the current time
typecatDisplays the contents of a file
wherewhichSearches the current directory and the PATH for a file or command
whoamiidDisplays the name and group of the current user

Batch Variables and Their Bash Equivalent

BatchBashDescription
%variable%$variableA regular variable
!variable!$variableA variable inside of a code block when setlocal enabledelayedexpansion is on
%errorlevel% or ERRORLEVEL$?Returns the status of the previous command: 0 if successful, 1 (or something else) if not
%1, %2, %3, etc.$1, $2, $3, etc.The parameters given to a script
%*$*All parameters given to a script