TODO: need syntax examples for Bash and Windows Batch scripting (issue #22)
- Add syntax examples for Bash and Windows Batch scripting
- Add example output for all
{% tabs %} {% tab title="Python" %}
| Type | Code Examples |
|---|---|
| Standard Variable | var = "Hello" |
| Global Variable |
|
| Environment Variables | |
| Retrieving Variable Contents | |
| {% endtab %} |
{% tab title="PowerShell" %}
| Type | Code Examples |
|---|---|
| Standard Variable | $var = "Hello" |
| Global Variable | $global:var = "Hello" |
| Environment Variables | |
| Retrieving Variable Contents | |
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Type | Code Examples |
|---|---|
| Standard Variable | |
| Global Variable | |
| Environment Variables | |
| Retrieving Variable Contents | |
| {% endtab %} |
{% tab title="CMD .bat" %} TODO: this
| Type | Code Examples |
|---|---|
| Standard Variable | |
| Global Variable | |
| Environment Variables | |
| Retrieving Variable Contents |
The other way in which variables can be initialized is via the ‘set’ command. Following is the syntax of the set command.
set /A variable-name=value
where,
- variable-name is the name of the variable you want to set.
- value is the value which needs to be set against the variable.
- /A – This switch is used if the value needs to be numeric in nature.
The following example shows a simple way the set command can be used.
@echo off
set message=Hello World
echo %message%
In batch script, it is also possible to define a variable to hold a numeric value. This can be done by using the /A switch.
The following code shows a simple way in which numeric values can be set with the /A switch.
@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
echo %c%
In any programming language, there is an option to mark variables as having some sort of scope, i.e. the section of code on which they can be accessed. Normally, variable having a global scope can be accessed anywhere from a program whereas local scoped variables have a defined boundary in which they can be accessed.
DOS scripting also has a definition for locally and globally scoped variables. By default, variables are global to your entire command prompt session. Call the SETLOCAL command to make variables local to the scope of your script. After calling SETLOCAL, any variable assignments revert upon calling ENDLOCAL, calling EXIT, or when execution reaches the end of file (EOF) in your script. The following example shows the difference when local and global variables are set in the script.
@echo off
set globalvar = 5
SETLOCAL
set var = 13145
set /A var = %var% + 5
echo %var%
echo %globalvar%
ENDLOCAL
Few key things to note about the above program.
- The ‘globalvar’ is defined with a global scope and is available throughout the entire script.
- The ‘var‘ variable is defined in a local scope because it is enclosed between a ‘SETLOCAL’ and ‘ENDLOCAL’ block. Hence, this variable will be destroyed as soon the ‘ENDLOCAL’ statement is executed.
If you have variables that would be used across batch files, then it is always preferable to use environment variables. Once the environment variable is defined, it can be accessed via the % sign. The following example shows how to see the JAVA_HOME defined on a system. The JAVA_HOME variable is a key component that is normally used by a wide variety of applications.
@echo off
echo %JAVA_HOME%
The output would show the JAVA_HOME directory which would depend from system to system. Following is an example of an output.
C:\Atlassian\Bitbucket\4.0.1\jre
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Method | Code Examples |
|---|---|
| Normal String |
|
| Empty String |
|
| Multiline String |
|
| Select Character from String |
|
| Get Length |
|
| Remove whitespace at front and back |
|
| To Lowercase |
|
| To Uppercase |
|
| Replace |
|
| Split |
|
| Join |
|
| Formatting |
|
| Formatting by Index |
|
| Formatting Strings |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Method | Code Examples |
|---|---|
| Normal String |
|
| Empty String |
|
| Multiline String |
|
| Select Character from String |
|
| Get Length |
|
| Remove whitespace at front and back |
|
| To Lowercase |
|
| To Uppercase |
|
| Replace |
|
| Split |
|
| Join |
|
| Formatting |
|
| Formatting by Index |
|
| Formatting Strings |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Method | Code Examples |
|---|---|
| Normal String | |
| Empty String | |
| Multiline String | |
| Select Character from String | |
| Get Length | |
| Remove whitespace at front and back | |
| To Lowercase | |
| To Uppercase | |
| Replace | |
| Split | |
| Join | |
| Formatting | |
| Formatting by Index | |
| Formatting Strings |
name="John"
echo ${name}
echo ${name/J/j} #=> "john" (substitution)
echo ${name:0:2} #=> "Jo" (slicing)
echo ${name::2} #=> "Jo" (slicing)
echo ${name::-1} #=> "Joh" (slicing)
echo ${name:(-1)} #=> "n" (slicing from right)
echo ${name:(-2):1} #=> "h" (slicing from right)
echo ${food:-Cake} #=> $food or "Cake"
length=2
echo ${name:0:length} #=> "Jo"
See: Parameter expansion
STR="/path/to/foo.cpp"
echo ${STR%.cpp} # /path/to/foo
echo ${STR%.cpp}.o # /path/to/foo.o
echo ${STR%/*} # /path/to
echo ${STR##*.} # cpp (extension)
echo ${STR##*/} # foo.cpp (basepath)
echo ${STR#*/} # path/to/foo.cpp
echo ${STR##*/} # foo.cpp
echo ${STR/foo/bar} # /path/to/bar.cpp
STR="Hello world"
echo ${STR:6:5} # "world"
echo ${STR: -5:5} # "world"
SRC="/path/to/foo.cpp"
BASE=${SRC##*/} #=> "foo.cpp" (basepath)
DIR=${SRC%$BASE} #=> "/path/to/" (dirpath)
${FOO:-val} |
$FOO, or val if unset (or null) |
|---|---|
${FOO:=val} |
Set $FOO to val if unset (or null) |
${FOO:+val} |
val if $FOO is set (and not null) |
${FOO:?message} |
Show error message and exit if $FOO is unset (or null) |
Omitting the : removes the (non)nullity checks, e.g. ${FOO-val} expands to val if unset otherwise $FOO.
${FOO%suffix} |
Remove suffix |
|---|---|
${FOO#prefix} |
Remove prefix |
${FOO%%suffix} |
Remove long suffix |
${FOO##prefix} |
Remove long prefix |
${FOO/from/to} |
Replace first match |
${FOO//from/to} |
Replace all |
${FOO/%from/to} |
Replace suffix |
${FOO/#from/to} |
Replace prefix |
${FOO:0:3} |
Substring (position, length) |
|---|---|
${FOO:(-3):3} |
Substring from the right |
${#FOO} |
Length of $FOO |
|---|
STR="HELLO WORLD!"
echo ${STR,} #=> "hELLO WORLD!" (lowercase 1st letter)
echo ${STR,,} #=> "hello world!" (all lowercase)
STR="hello world!"
echo ${STR^} #=> "Hello world!" (uppercase 1st letter)
echo ${STR^^} #=> "HELLO WORLD!" (all uppercase)
NAME="John"
echo "Hi $NAME" #=> Hi John
echo 'Hi $NAME' #=> Hi $NAME
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Method | Code Examples |
|---|---|
| Normal String | |
| Empty String | |
| Multiline String | |
| Select Character from String | |
| Get Length | |
| Remove whitespace at front and back | |
| To Lowercase | |
| To Uppercase | |
| Replace | |
| Split | |
| Join | |
| Formatting | |
| Formatting by Index | |
| Formatting Strings |
An empty string can be created in DOS Scripting by assigning it no value during it’s initialization as shown in the following example.
Set a=
To check for an existence of an empty string, you need to encompass the variable name in square brackets and also compare it against a value in square brackets as shown in the following example.
[%a%]==[]
The following example shows how an empty string can be created and how to check for the existence of an empty string.
@echo off
SET a=
SET b=Hello
if [%a%]==[] echo "String A is empty"
if [%b%]==[] echo "String B is empty "
A string can be created in DOS in the following way.
@echo off
:: This program just displays Hello World
set message = Hello World
echo %message%
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Type | Code Examples |
|---|---|
| As Integer | i = int("10") |
| As Float | i = float("10.5") |
| As String | i = str(10) |
| As Char | |
| {% endtab %} |
{% tab title="PowerShell" %}
| Type | Code Examples |
|---|---|
| As Integer | $i = [int]"10" |
| As Float | $i = [float]"10.5" |
| As String | $i = [string]10 |
| As Char | |
| {% endtab %} |
{% tab title="Bash" %}
| Type | Code Examples |
|---|---|
| As Integer | |
| As Float | |
| As String | |
| As Char | |
| {% endtab %} |
{% tab title="CMD .bat" %}
| Type | Code Examples |
|---|---|
| As Integer | |
| As Float | |
| As String | |
| As Char | |
| {% endtab %} | |
| {% endtabs %} |
{% tabs %} {% tab title="Python" %}
| Activity | Code examples |
|---|---|
| Define | ['Hello', 'World'] |
| Access Elements |
|
| Get Length |
|
| Adding Elements |
|
| Removing Elements |
|
| Remove Element by Value |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Activity | Code examples |
|---|---|
| Define | @('Hello', 'World') |
| Access Elements |
|
| Get Length |
|
| Adding Elements |
|
| Removing Elements |
|
| Remove Element by Value |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Activity | Code examples |
|---|---|
| Define | |
| Access Elements | |
| Get Length | |
| Adding Elements | |
| Removing Elements | |
| Remove Element by Value |
echo ${Fruits[0]} # Element #0
echo ${Fruits[-1]} # Last element
echo ${Fruits[@]} # All elements, space-separated
echo ${#Fruits[@]} # Number of elements
echo ${#Fruits} # String length of the 1st element
echo ${#Fruits[3]} # String length of the Nth element
echo ${Fruits[@]:3:2} # Range (from position 3, length 2)
echo ${!Fruits[@]} # Keys of all elements, space-separated
Fruits=("${Fruits[@]}" "Watermelon") # Push
Fruits+=('Watermelon') # Also Push
Fruits=( ${Fruits[@]/Ap*/} ) # Remove by regex match
unset Fruits[2] # Remove one item
Fruits=("${Fruits[@]}") # Duplicate
Fruits=("${Fruits[@]}" "${Veggies[@]}") # Concatenate
lines=(`cat "logfile"`) # Read from file
for i in "${arrayName[@]}"; do
echo $i
done
Fruits=('Apple' 'Banana' 'Orange')
Fruits[0]="Apple"
Fruits[1]="Banana"
Fruits[2]="Orange"
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Activity | Code examples |
|---|---|
| Define | |
| Access Elements | |
| Get Length | |
| Adding Elements | |
| Removing Elements | |
| Remove Element by Value |
Arrays are not specifically defined as a type in Batch Script but can be implemented. The following things need to be noted when arrays are implemented in Batch Script.
- Each element of the array needs to be defined with the set command.
- The ‘for’ loop would be required to iterate through the values of the array.
An array is created by using the following set command.
set a[0]=1
Where 0 is the index of the array and 1 is the value assigned to the first element of the array.
Another way to implement arrays is to define a list of values and iterate through the list of values. The following example show how this can be implemented.
@echo off
set list = 1 2 3 4
(for %%a in (%list%) do (
echo %%a
))
The above command produces the following output.
1
2
3
4
You can retrieve a value from the array by using subscript syntax, passing the index of the value you want to retrieve within square brackets immediately after the name of the array.
@echo off
set a[0]=1
echo %a[0]%
In this example, the index starts from 0 which means the first element can be accessed using index as 0, the second element can be accessed using index as 1 and so on. Let's check the following example to create, initialize and access arrays −
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
echo The first element of the array is %a[0]%
echo The second element of the array is %a[1]%
echo The third element of the array is %a[2]%
The above command produces the following output.
The first element of the array is 1
The second element of the array is 2
The third element of the array is 3
To add an element to the end of the array, you can use the set element along with the last index of the array element.
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
Rem Adding an element at the end of an array
Set a[3] = 4
echo The last element of the array is %a[3]%
The above command produces the following output.
The last element of the array is 4
You can modify an existing element of an Array by assigning a new value at a given index as shown in the following example −
@echo off
set a[0] = 1
set a[1] = 2
set a[2] = 3
Rem Setting the new value for the second element of the array
Set a[1] = 5
echo The new value of the second element of the array is %a[1]%
The above command produces the following output.
The new value of the second element of the array is 5
Iterating over an array is achieved by using the ‘for’ loop and going through each element of the array. The following example shows a simple way that an array can be implemented.
@echo off
setlocal enabledelayedexpansion
set topic[0] = comments
set topic[1] = variables
set topic[2] = Arrays
set topic[3] = Decision making
set topic[4] = Time and date
set topic[5] = Operators
for /l %%n in (0,1,5) do (
echo !topic[%%n]!
)
Following things need to be noted about the above program −
- Each element of the array needs to be specifically defined using the set command.
- The ‘for’ loop with the /L parameter for moving through ranges is used to iterate through the array.
The above command produces the following output.
Comments
variables
Arrays
Decision making
Time and date
Operators
The length of an array is done by iterating over the list of values in the array since there is no direct function to determine the number of elements in an array.
@echo off
set Arr[0] = 1
set Arr[1] = 2
set Arr[2] = 3
set Arr[3] = 4
set "x = 0"
:SymLoop
if defined Arr[%x%] (
call echo %%Arr[%x%]%%
set /a "x+=1"
GOTO :SymLoop
)
echo "The length of the array is" %x%
Output The above command produces the following output.
The length of the array is 4
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Switch | Code Examples |
|---|---|
| If / ElseIf / Else |
|
| Case | |
| {% endtab %} |
{% tab title="PowerShell" %}
| Switch | Code Examples |
|---|---|
| If / ElseIf / Else |
|
| Case | |
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Switch | Code Examples |
|---|---|
| If / ElseIf / Else | |
| Case |
case "$1" in
start | up)
vagrant up
;;
*)
echo "Usage: $0 {start|stop|ssh}"
;;
esac
# String
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
else
echo "This never happens"
fi
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Switch | Code Examples |
|---|---|
| If / ElseIf / Else | |
| Case |
The first decision-making statement is the ‘if’ statement. The general form of this statement in Batch Script is as follows −
if(condition) do_something
The general working of this statement is that first a condition is evaluated in the ‘if’ statement. If the condition is true, it then executes the statements. The following diagram shows the flow of the if statement.
One of the common uses for the ‘if’ statement in Batch Script is for checking variables which are set in Batch Script itself. The evaluation of the ‘if’ statement can be done for both strings and numbers.
The following example shows how the ‘if’ statement can be used for numbers.
Example
@echo off
SET /A a = 5
SET /A b = 10
SET /A c = %a% + %b%
if %c%==15 echo "The value of variable c is 15"
if %c%==10 echo "The value of variable c is 10"
The key thing to note about the above program is −
- The first ‘if’ statement checks if the value of the variable c is 15. If so, then it echo’s a string to the command prompt.
- Since the condition in the statement - if %c% == 10 echo "The value of variable c is 10 evaluates to false, the echo part of the statement will not be executed.
Output
The above command produces the following output.
15
The following example shows how the ‘if’ statement can be used for strings.
Example
@echo off
SET str1 = String1
SET str2 = String2
if %str1%==String1 echo "The value of variable String1"
if %str2%==String3 echo "The value of variable c is String3"
The key thing to note about the above program is −
- The first ‘if’ statement checks if the value of the variable str1 contains the string “String1”. If so, then it echo’s a string to the command prompt.
- Since the condition of the second ‘if’ statement evaluates to false, the echo part of the statement will not be executed.
Output
The above command produces the following output.
"The value of variable String1"
Note − One key thing to note is that the evaluation in the ‘if’ statement is "case-sensitive”. The same program as above is modified a little as shown in the following example. In the first statement, we have changed the comparison criteria. Because of the different casing, the output of the following program would yield nothing.
@echo off
SET str1 = String1
SET str2 = String2
if %str1%==StrinG1 echo "The value of variable String1"
if %str2%==String3 echo "The value of variable c is String3"
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Loop Type | Code Examples |
|---|---|
| For |
|
| While |
|
| Break |
|
| Continue |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Loop Type | Code Examples |
|---|---|
| For |
|
| While |
|
| Break |
|
| Continue |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Loop Type | Code Examples |
|---|---|
| For | |
| While | |
| Break | |
| Continue |
for i in /etc/rc.*; do
echo $i
done
for ((i = 0 ; i < 100 ; i++)); do
echo $i
done
for i in {1..5}; do
echo "Welcome $i"
done
With step size
for i in {5..50..5}; do
echo "Welcome $i"
done
cat file.txt | while read line; do
echo $line
done
while true; do
···
done
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Loop Type | Code Examples |
|---|---|
| For | |
| While | |
| Break | |
| Continue |
In the decision making chapter, we have seen statements which have been executed one after the other in a sequential manner. Additionally, implementations can also be done in Batch Script to alter the flow of control in a program’s logic. They are then classified into flow of control statements.
There is no direct while statement available in Batch Script but we can do an implementation of this loop very easily by using the if statement and labels.
There is no direct while statement available in Batch Script but we can do an implementation of this loop very easily by using the if statement and labels.
The first part of the while implementation is to set the counters which will be used to control the evaluation of the ‘if’ condition. We then define our label which will be used to embody the entire code for the while loop implementation. The ‘if’ condition evaluates an expression. If the expression evaluates to true, the code block is executed. If the condition evaluates to false then the loop is exited. When the code block is executed, it will return back to the label statement for execution again.
Following is the syntax of the general implementation of the while statement.
Set counters
:label
If (expression) (
Do_something
Increment counter
Go back to :label
)
- The entire code for the while implementation is placed inside of a label.
- The counter variables must be set or initialized before the while loop implementation starts.
- The expression for the while condition is done using the ‘if’ statement. If the expression evaluates to true then the relevant code inside the ‘if’ loop is executed.
- A counter needs to be properly incremented inside of ‘if’ statement so that the while implementation can terminate at some point in time.
- Finally, we will go back to our label so that we can evaluate our ‘if’ statement again.
Following is an example of a while loop statement.
@echo off
SET /A "index = 1"
SET /A "count = 5"
:while
if %index% leq %count% (
echo The value of index is %index%
SET /A "index = index + 1"
goto :while
)
In the above example, we are first initializing the value of an index integer variable to 1. Then our condition in the ‘if’ loop is that we are evaluating the condition of the expression to be that index should it be less than the value of the count variable. Till the value of index is less than 5, we will print the value of index and then increment the value of index.
The "FOR" construct offers looping capabilities for batch files. Following is the common construct of the ‘for’ statement for working with a list of values.
FOR %%variable IN list DO do_something
The classic ‘for’ statement consists of the following parts −
- Variable declaration – This step is executed only once for the entire loop and used to declare any variables which will be used within the loop. In Batch Script, the variable declaration is done with the %% at the beginning of the variable name.
- List – This will be the list of values for which the ‘for’ statement should be executed.
- The do_something code block is what needs to be executed for each iteration for the list of values.
Following is an example of how the ‘goto’ statement can be used.
@echo off
FOR %%F IN (1 2 3 4 5) DO echo %%F
The key thing to note about the above program is −
- The variable declaration is done with the %% sign at the beginning of the variable name.
- The list of values is defined after the IN clause.
- The do_something code is defined after the echo command. Thus for each value in the list, the echo command will be executed.
The ‘for’ statement also has the ability to move through a range of values. Following is the general form of the statement.
FOR /L %%variable IN (lowerlimit,Increment,Upperlimit) DO do_something
Where
- The /L switch is used to denote that the loop is used for iterating through ranges.
- Variable declaration – This step is executed only once for the entire loop and used to declare any variables which will be used within the loop. In Batch Script, the variable declaration is done with the %% at the beginning of the variable name.
- The IN list contains of 3 values. The lowerlimit, the increment, and the upperlimit. So, the loop would start with the lowerlimit and move to the upperlimit value, iterating each time by the Increment value.
- The do_something code block is what needs to be executed for each iteration.
Following is an example of how the looping through ranges can be carried out.
@ECHO OFF
FOR /L %%X IN (0,1,5) DO ECHO %%X
Following is the classic ‘for’ statement which is available in most programming languages.
for(variable declaration;expression;Increment) {
statement #1
statement #2
…
}
The Batch Script language does not have a direct ‘for’ statement which is similar to the above syntax, but one can still do an implementation of the classic ‘for’ loop statement using if statements and labels.
Let’s look at the general syntax implementation of the classic for loop in batch scripting.
Set counter
:label
If (expression) exit loop
Do_something
Increment counter
Go back to :label
- The entire code for the ‘for’ implementation is placed inside of a label.
- The counters variables must be set or initialized before the ‘for’ loop implementation starts.
- The expression for the ‘for’ loop is done using the ‘if’ statement. If the expression evaluates to be true then an exit is executed to come out of the loop.
- A counter needs to be properly incremented inside of the ‘if’ statement so that the ‘for’ implementation can continue if the expression evaluation is false.
- Finally, we will go back to our label so that we can evaluate our ‘if’ statement again.
Following is an example of how to carry out the implementation of the classic ‘for’ loop statement.
@echo off
SET /A i = 1
:loop
IF %i%==5 GOTO END
echo The value of i is %i%
SET /a i=%i%+1
GOTO :LOOP
:END
The ‘for’ statement can also be used for checking command line arguments. The following example shows how the ‘for’ statement can be used to loop through the command line arguments.
@ECHO OFF
:Loop
IF "%1"=="" GOTO completed
FOR %%F IN (%1) DO echo %%F
SHIFT
GOTO Loop
:completed
Let’s assume that our above code is stored in a file called Test.bat. The above command will produce the following output if the batch file passes the command line arguments of 1,2 and 3 as Test.bat 1 2 3.
1
2
3
The break statement is used to alter the flow of control inside loops within any programming language. The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost enclosing loop.
The break statement is used to alter the flow of control inside loops within any programming language. The break statement is normally used in looping constructs and is used to cause immediate termination of the innermost enclosing loop.
The Batch Script language does not have a direct ‘for’ statement which does a break but this can be implemented by using labels. The following diagram shows the diagrammatic explanation of the break statement implementation in Batch Script.
@echo off
SET /A "index=1"
SET /A "count=5"
:while
if %index% leq %count% (
if %index%==2 goto :Increment
echo The value of index is %index%
:Increment
SET /A "index=index + 1"
goto :while
)
The key thing to note about the above implementation is the involvement of two ‘if’ conditions. The second ‘if’ condition is used to control when the break is implemented. If the second ‘if’ condition is evaluated to be true, then the code block is not executed and the counter is directly implemented.
Following is an example of how to carry out the implementation of the break statement.
The key thing to note about the above program is the addition of a label called :Increment. When the value of index reaches 2, we want to skip the statement which echoes its value to the command prompt and directly just increment the value of index. {% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Functions | Code Examples |
|---|---|
| Definition |
<code></code>
|
| Arguments |
<code></code>
|
| Variable Arguments |
<code></code>
|
| Named Arguments |
<code></code>
|
| Default Values |
<code></code>
|
| Return Values |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Functions | Code Examples |
|---|---|
| Definition |
|
| Arguments |
|
| Variable Arguments |
|
| Named Arguments |
|
| Default Values |
<code></code>
|
| Return Values |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Functions | Code Examples |
|---|---|
| Definition | |
| Arguments | |
| Variable Arguments | |
| Named Arguments | |
| Default Values | |
| Return Values |
$# |
Number of arguments |
|---|---|
$* |
All arguments |
$@ |
All arguments, starting from first |
$1 |
First argument |
$_ |
Last argument of the previous command |
myfunc() {
local myresult='some value'
echo $myresult
}
result="$(myfunc)"
Defining Functions
myfunc() {
echo "hello $1"
}
# Same as above (alternate syntax)
function myfunc() {
echo "hello $1"
}
myfunc "John"
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Functions | Code Examples |
|---|---|
| Definition | |
| Arguments | |
| Variable Arguments | |
| Named Arguments | |
| Default Values | |
| Return Values | |
| {% endtab %} | |
| {% endtabs %} |
{% tabs %} {% tab title="Python" %}
| Activity | Code Examples |
|---|---|
| Class Definition |
|
| Object Creation | MyClass() |
| Using Class Constructors |
<code></code>
|
| Defining and using Methods |
<code></code> <code></code>
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Activity | Code Examples |
|---|---|
| Class Definition |
|
| Object Creation | [MyClass]::new() |
| Using Class Constructors |
<code></code>
|
| Defining and using Methods |
<code></code> <code></code>
<code></code>
|
| {% endtab %} |
{% tab title="Bash" %}
| Activity | Code Examples |
|---|---|
| Class Definition | |
| Object Creation | |
| Using Class Constructors | |
| Defining and using Methods | |
| {% endtab %} |
{% tab title="CMD .bat" %}
| Activity | Code Examples |
|---|---|
| Class Definition | |
| Object Creation | |
| Using Class Constructors | |
| Defining and using Methods | |
| {% endtab %} | |
| {% endtabs %} |
{% tabs %} {% tab title="Python" %}
| Comment Type | Code Examples |
|---|---|
| Single line | # Hello, world! |
| Multiline |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Comment Type | Code Examples |
|---|---|
| Single line | # Hello, world! |
| Multiline |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Comment Type | Code Examples |
|---|---|
| Single line | |
| Multiline |
# Single line comment
: '
This is a
multi line
comment
'
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Comment Type | Code Examples |
|---|---|
| Single line | |
| Multiline |
There are two ways to create comments in Batch Script; one is via the Rem command. Any text which follows the Rem statement will be treated as comments and will not be executed. Following is the general syntax of this statement.
Rem Remarks
where ‘Remarks’ is the comments which needs to be added.
The following example shows a simple way the Rem command can be used.
@echo off
Rem This program just displays Hello World
set message=Hello World
echo %message%
The above command produces the following output. You will notice that the line with the Rem statement will not be executed.
Hello World
The other way to create comments in Batch Script is via the :: command. Any text which follows the :: statement will be treated as comments and will not be executed. Following is the general syntax of this statement.
:: Remarks
where ‘Remarks’ is the comment which needs to be added.
The following example shows the usage of the "::" command.
@echo off
:: This program just displays Hello World
set message = Hello World
echo %message%
The above command produces the following output. You will notice that the line with the :: statement will not be executed.
Hello World
Note − If you have too many lines of Rem, it could slow down the code, because in the end each line of code in the batch file still needs to be executed. {% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Action | Code Examples |
|---|---|
| Get Object's Type |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Action | Code Examples |
|---|---|
| Get Object's Type |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Action | Code Examples |
|---|---|
| Get Object's Type | |
| {% endtab %} |
{% tab title="CMD .bat" %} TODO: this
| Action | Code Examples |
|---|---|
| Get Object's Type | |
| {% endtab %} | |
| {% endtabs %} |
{% tabs %} {% tab title="Python" %}
| Activity | Code Examples |
|---|---|
| Defining |
|
| Accessing Elements |
|
| Updating Elements |
|
| Enumerating Keys |
|
| Enumerating Values |
|
| Check if key exists |
|
| Adding items |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Activity | Code Examples |
|---|---|
| Defining |
|
| Accessing Elements |
<code></code>
<code></code>
|
| Updating Elements |
|
| Enumerating Keys |
|
| Enumerating Values |
|
| Check if key exists |
|
| Adding items |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Activity | Code Examples |
|---|---|
| Defining | |
| Accessing Elements | |
| Updating Elements | |
| Enumerating Keys | |
| Enumerating Values | |
| Check if key exists | |
| Adding items |
declare -A sounds
sounds[dog]="bark"
sounds[cow]="moo"
sounds[bird]="tweet"
sounds[wolf]="howl"
Declares sound as a Dictionary object (aka associative array).
echo ${sounds[dog]} # Dog's sound
echo ${sounds[@]} # All values
echo ${!sounds[@]} # All keys
echo ${#sounds[@]} # Number of elements
unset sounds[dog] # Delete dog
Iterate over values
for val in "${sounds[@]}"; do
echo $val
done
Iterate over keys
for key in "${!sounds[@]}"; do
echo $key
done
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Activity | Code Examples |
|---|---|
| Defining | |
| Accessing Elements | |
| Updating Elements | |
| Enumerating Keys | |
| Enumerating Values | |
| Check if key exists | |
| Adding items |
Structures can also be implemented in batch files using a little bit of an extra coding for implementation. The following example shows how this can be achieved.
@echo off
set len = 3
set obj[0].Name = Joe
set obj[0].ID = 1
set obj[1].Name = Mark
set obj[1].ID = 2
set obj[2].Name = Mohan
set obj[2].ID = 3
set i = 0
:loop
if %i% equ %len% goto :eof
set cur.Name=
set cur.ID=
for /f "usebackq delims==.tokens=1-3" %%j in (`set obj[%i%]`) do (
set cur.%%k=%%l
)
echo Name = %cur.Name%
echo Value = %cur.ID%
set /a i = %i%+1
goto loop
The following key things need to be noted about the above code.
- Each variable defined using the set command has 2 values associated with each index of the array.
- The variable i is set to 0 so that we can loop through the structure will the length of the array which is 3.
- We always check for the condition on whether the value of i is equal to the value of len and if not, we loop through the code.
- We are able to access each element of the structure using the obj[%i%] notation.
The above command produces the following output.
Name = Joe
Value = 1
Name = Mark
Value = 2
Name = Mohan
Value = 3
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Lambda | Code Examples |
|---|---|
| Lambda |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Lambda | Code Examples |
|---|---|
| Lambda |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Lambda | Code Examples |
|---|---|
| Lambda | |
| {% endtab %} |
{% tab title="CMD .bat" %} TODO: this
| Lambda | Code Examples |
|---|---|
| Lambda | |
| {% endtab %} | |
| {% endtabs %} |
TODO: Add other operator types
{% tabs %} {% tab title="Python" %}
| Operator | Code Examples |
|---|---|
| Addition | var = 1 + 1 |
| Subtraction | var = 1 - 1 |
| Multiplication | var = 1 * 1 |
| Division | var = 1 / 1 |
| Modulus | var = 1 % 1 |
| Floor | var = 10 // 3 |
| Exponent | var = 10 ** 3 |
| {% endtab %} |
{% tab title="PowerShell" %}
| Operator | Code Examples |
|---|---|
| Addition | $var = 1 + 1 |
| Subtraction | $var = 1 - 1 |
| Multiplication | $var = 1 * 1 |
| Division | $var = 1 / 1 |
| Modulus | $var = 1 % 1 |
| Floor | $var = [Math]::Floor(10 / 3) |
| Exponent | $var = [Math]::Pow(10, 3) |
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Operator | Code Examples |
|---|---|
| Addition | |
| Subtraction | |
| Multiplication | |
| Division | |
| Modulus | |
| Floor | |
| Exponent | |
| {% endtab %} |
{% tab title="CMD .bat" %} TODO: this
| Operator | Code Examples |
|---|---|
| Addition | |
| Subtraction | |
| Multiplication | |
| Division | |
| Modulus | |
| Floor | |
| Exponent |
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations.
In batch script, the following types of operators are possible.
- Arithmetic operators
- Relational operators
- Logical operators
- Assignment operators
- Bitwise operators
Batch script language supports the normal Arithmetic operators as any language. Following are the Arithmetic operators available.
| Operator | Description | Example |
|---|---|---|
| + | Addition of two operands | 1 + 2 will give 3 |
| − | Subtracts second operand from the first | 2 − 1 will give 1 |
| * | Multiplication of both operands | 2 * 2 will give 4 |
| / | Division of the numerator by the denominator | 3 / 2 will give 1.5 |
| % | Modulus operator and remainder of after an integer/float division | 3 % 2 will give 1 |
Relational operators allow of the comparison of objects. Below are the relational operators available.
| Operator | Description | Example |
|---|---|---|
| EQU | Tests the equality between two objects | 2 EQU 2 will give true |
| NEQ | Tests the difference between two objects | 3 NEQ 2 will give true |
| LSS | Checks to see if the left object is less than the right operand | 2 LSS 3 will give true |
| LEQ | Checks to see if the left object is less than or equal to the right operand | 2 LEQ 3 will give true |
| GTR | Checks to see if the left object is greater than the right operand | 3 GTR 2 will give true |
| GEQ | Checks to see if the left object is greater than or equal to the right operand | 3 GEQ 2 will give true |
Logical operators are used to evaluate Boolean expressions. Following are the logical operators available.
The batch language is equipped with a full set of Boolean logic operators like AND, OR, XOR, but only for binary numbers. Neither are there any values for TRUE or FALSE. The only logical operator available for conditions is the NOT operator.
| Operator | Description |
|---|---|
| AND | This is the logical “and” operator |
| OR | This is the logical “or” operator |
| NOT | This is the logical “not” operator |
Batch Script language also provides assignment operators. Following are the assignment operators available.
| Operator | Description | Example |
|---|---|---|
| += | This adds right operand to the left operand and assigns the result to left operand | Set /A a = 5 a += 3 Output will be 8 |
| -= | This subtracts the right operand from the left operand and assigns the result to the left operand | Set /A a = 5 a -= 3 Output will be 2 |
| *= | This multiplies the right operand with the left operand and assigns the result to the left operand | Set /A a = 5 a *= 3 Output will be 15 |
| /= | This divides the left operand with the right operand and assigns the result to the left operand | Set /A a = 6 a/ = 3 Output will be 2 |
| %= | This takes modulus using two operands and assigns the result to the left operand | Set /A a = 5 a% = 3 Output will be 2 |
Bitwise operators are also possible in batch script. Following are the operators available.
| Operator | Description |
|---|---|
| & | This is the bitwise “and” operator |
| | | This is the bitwise “or” operator |
| ^ | This is the bitwise “xor” or Exclusive or operator |
Following is the truth table showcasing these operators.
| p | q | p & q | p | q | p ^ q |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
| 1 | 0 | 0 | 1 | 1 |
| {% endtab %} | ||||
| {% endtabs %} |
{% tabs %} {% tab title="Python" %}
| Error Handling | Code Examples |
|---|---|
| Try/Except |
|
| Else |
|
| Finally |
|
| Raise |
|
| {% endtab %} |
{% tab title="PowerShell" %}
| Error Handling | Code Examples |
|---|---|
| Try/Catch |
|
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Error Handling | Code Examples |
|---|---|
| Try/Catch |
trap 'echo Error at about $LINENO' ERR
or
traperr() {
echo "ERROR: ${BASH_SOURCE[1]} at about ${BASH_LINENO[0]}"
}
set -o errtrace
trap traperr ERR
myfunc() {
return 1
}
if myfunc; then
echo "success"
else
echo "failure"
fi
{% endtab %}
{% tab title="CMD .bat" %} TODO: this
| Error Handling | Code Examples |
|---|---|
| Try/Catch | |
| {% endtab %} | |
| {% endtabs %} |
{% tabs %} {% tab title="Python" %}
{% endtab %}
{% tab title="PowerShell" %}
To execute regular Windows shell commands (from cmd.exe) in PowerShell, simply type the command the same way you would in the Windows command shell. Some commands may not work in the same way, and some may need the full filename (example: to se a directory listing in cmd.exe dir is the command. To use this in PowerShell you would need to specify dir.exe.
IEX (Invoke-Expression) {% endtab %}
{% tab title="Bash" %}
pwd
echo "I'm in $(pwd)"
echo "I'm in `pwd`"
{% endtab %}
{% tab title="CMD .bat" %}
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
{% endtab %}
{% tab title="PowerShell" %}
2>null
Many cmdlets also have an ErrorAction property
-ErrorAction Silent
{% endtab %}
{% tab title="Bash" %} Redirect Standard Error to the nether
2>/dev/null
{% endtab %}
{% tab title="CMD .bat" %} There are three universal “files” for keyboard input, printing text on the screen and printing errors on the screen. The “Standard In” file, known as stdin, contains the input to the program/script. The “Standard Out” file, known as stdout, is used to write output for display on the screen. Finally, the “Standard Err” file, known as stderr, contains any error messages for display on the screen.
Each of these three standard files, otherwise known as the standard streams, are referenced using the numbers 0, 1, and 2. Stdin is file 0, stdout is file 1, and stderr is file 2.
One common practice in batch files is sending the output of a program to a log file. The > operator sends, or redirects, stdout or stderr to another file. The following example shows how this can be done.
Dir C:\ > list.txt
In the above example, the stdout of the command Dir C: is redirected to the file list.txt.
If you append the number 2 to the redirection filter, then it would redirect the stderr to the file lists.txt.
Dir C:\ 2> list.txt
One can even combine the stdout and stderr streams using the file number and the ‘&’ prefix. Following is an example.
DIR C:\ > lists.txt 2>&1
The pseudo file NUL is used to discard any output from a program. The following example shows that the output of the command DIR is discarded by sending the output to NUL.
Dir C:\ > NUL
To work with the Stdin, you have to use a workaround to achieve this. This can be done by redirecting the command prompt’s own stdin, called CON.
The following example shows how you can redirect the output to a file called lists.txt. After you execute the below command, the command prompt will take all the input entered by user till it gets an EOF character. Later, it sends all the input to the file lists.txt.
TYPE CON > lists.txt
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
{% endtab %}
{% tab title="PowerShell" %}
{% endtab %}
{% tab title="Bash" %}
cat <<HERE
hello world
HERE
{% endtab %}
{% tab title="CMD .bat" %}
{% endtab %} {% endtabs %}
{% tabs %} {% tab title="Python" %}
| Activity | Code Examples |
|---|---|
| Install | pip install requests |
| Import | import requests |
| List | pip list |
| {% endtab %} |
{% tab title="PowerShell" %}
| Activity | Code Examples |
|---|---|
| Install | Install-Module Pester |
| Import | Import-Module Pester |
| List | Get-Module -ListAvailable |
| {% endtab %} |
{% tab title="Bash" %} TODO: this
| Activity | Code Examples |
|---|---|
| Install | |
| Import | |
| List | |
| {% endtab %} |
{% tab title="CMD .bat" %} TODO: this
| Activity | Code Examples |
|---|---|
| Install | |
| Import | |
| List | |
| {% endtab %} | |
| {% endtabs %} |
References
- https://devhints.io/bash
- https://wiki.bash-hackers.org/syntax/expansion/cmdsubst
- https://www.tutorialspoint.com/batch_script/batch_script_syntax.htm
If you like this content and would like to see more, please consider buying me a coffee!