Sub topic 3
Comments and Variables
This chapter assumes you can open a Windows command prompt and a Linux shell, and that you know what a file path is. You have already seen the script file types (.bat, .ps1, .sh, .py). Here you will learn how to read two constructs that appear in all of them: comments and variables.
Comments
A comment is text that the interpreter ignores. It explains what the script does, or it disables a line without deleting it. Each language marks comments differently.
| Language | File type | Comment marker |
|---|---|---|
| Batch | .bat | `REM` (also `::`) |
| PowerShell | .ps1 | `#` |
| Bash | .sh | `#` |
| Python | .py | `#` |
On a Linux script, the first line #!/bin/bash looks like a comment, but it is a shebang. It tells the system which interpreter should run the file. Every other line starting with # is an ordinary comment.
Variables
A variable is a named place that stores a value so the script can reuse it. You assign a value once and refer to the name afterward. If a path changes, you edit one line instead of many.
The syntax for setting and reading a variable differs by language.
| Language | Set a variable | Read a variable |
|---|---|---|
| Batch | `set name=value` | `%name%` |
| PowerShell | `$name = "value"` | `$name` |
| Bash | `name="value"` (no spaces around `=`) | `$name` |
| Python | `name = "value"` | `name` |
Worked example
This Bash script from the backup scenario uses both comments and variables.
#!/bin/bash
# Copy text files from Documents to a backup folder
src="$HOME/Documents"
dest="/mnt/backup"
for f in "$src"/*.txt; do
cp "$f" "$dest"
done
echo "Backup finished for user $USER"Read it line by line:
- The shebang selects Bash.
- The comment states the purpose.
srcanddestare variables that hold folder paths.$HOMEis a variable the system already provides.- The loop uses
$srcand$destinstead of repeating the full paths.
The same idea in Python looks like this:
name = "workstation01" # string
count = 3 # integer
print(f"{name}: {count} checks")Here the comment sits after the code on the same line. Everything from # to the end of the line is ignored.
Common errors
- Spaces in Bash assignments.
name = "x"fails because Bash treatsnameas a command. Writename="x". - Missing prefix. In PowerShell you must write
$nameto read a variable. In Bash you need$when reading but not when assigning. - Wrong percent signs in Batch. Reading a variable needs
%name%with a percent sign on both sides. - Unquoted paths. A path with spaces, such as
C:\Program Files, splits into two pieces unless it is quoted.
Exercise
A script contains the lines REM Map the drive and set drive=Z:. Which language is it, what does the first line do, and how would the script read the variable?
Show answer
It is a Windows batch script (.bat). The REM line is a comment, so it is ignored when the script runs. The script reads the variable with %drive%.
Exercise
In a Bash script, why does count = 5 fail while count=5 works?
Show answer
Bash does not allow spaces around = in an assignment. With spaces, it tries to run count as a command and passes = and 5 as arguments.