Differences between windows batch and linux bash shell script syntax

Carrying on from my previous post about using scripts created in Windows on Linux, here are some comparisons to show the syntax differences between Windows batch scripts and bash scripts.

Comments

Windows batch script

REM some comment

or

:: some comment

Linux shell script

# some comment

For loop through files in directory

Windows batch script

FOR %%i IN (%1\*) DO (
REM do stuff here
)

Linux shell script

for i in "$1"/*
do
# do stuff here
done

(%1 in batch and $1 in bash refer to the first argument passed in when running the script. E.g. running myscript.sh ./mydir, then in the script $1 would have the value of ./mydir.)

Setting a variable to a number and then printing the variable

Windows batch script

SET /A varInteger=0
echo The value of varInteger is %varInteger%

Linux shell script

varInteger=0
echo "The value of varInteger is $varInteger"

Execute a command and store the output in a variable

Windows batch script

FOR /F %%x IN ('command to be executed') DO SET result=%%x

Linux shell script

result=`command to be executed`

If statement

Windows batch script

IF %somevar% GEQ %someothervar% (
REM do something
)

Linux shell script

if [ $somevar -ge $someothervar ]
then
# do something
fi

(GEQ and -ge is the greater than or equal to comparison operator, both bash and windows batch scripts will let you use a range of comparison operators.)

While loop

Windows batch script

:loop
IF !somevar! GEQ %someothervar% GOTO endofloop
REM do something that increases the value of !somevar!
GOTO loop
:endofloop

Linux shell script

while [ ! $somevar -ge $someothervar ]
do
# do something that increases the value of $somevar
done

(If you’re wondering why I used !somevar! instead of %somevar% in the windows script, this is because in a while loop you will be wanting to update the variable. See this article for more info: Windows batch scripting: EnableDelayedExpansion.)

Check last command executed OK, if not print error message and exit

Windows batch script

somecommand
IF %ERRORLEVEL% NEQ 0 (
	echo "somecommand failed, exiting"
	GOTO :EOF
)
REM the EOF label should be located at the end of the file
:EOF

Linux shell script

somecommand
if [ $? -ne 0 ]
then
	echo "somecommand failed, exiting"
	exit 1
fi

Zeropad a number to 4 digits

Windows batch script

SET zeropadded=000%somenumber%
REM Trim zeropadded to only four digits, from the end
echo "4 digit padded = %zeropadded:~-4%"

Linux shell script

zeropadded=`printf "%04d" $somenumber`
echo "4 digit padded = $zeropadded"
Posted on by xoogu, last updated

6 Responses to “Differences between windows batch and linux bash shell script syntax”

  1. sudheer says:

    Good description and nice explanation.

  2. ___rrryannn___ says:

    I’m a beginner with shell and batch scripting, and this is an awesome resource!

    Concise and relevant. Thanks so much.

  3. B says:

    Thanks for the writing!

    I think you missed an “/F” option in this command:

    FOR %%x IN (‘command to be executed’) DO SET result=%%x

  4. archanish says:

    i have some batch and power shell scripts which i need to convert to shell scripts. plz help…….

    • xoogu says:

      Hi archanish

      What batch & power shell statements / control structures are you having trouble finding the shell equivalents for? And are you converting to bourne shell or bash?

      Dave

Leave a Reply