Python vs Shell: Full Comparison Guide (Beginner to Intermediate, 100 Topics)
This expanded guide includes 100 scripting topics comparing Python and Shell side by side. Each section
includes:
- Clear description of the concept
- Real working code examples with comments
- Detailed comparison of syntax and purpose
At the end of this guide, 30+ references are provided for further learning.
Topic 1: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 2: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 3: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 4: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 5: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 6: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 7: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 8: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 9: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 10: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 11: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 12: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 13: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 14: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 15: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 16: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 17: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 18: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 19: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 20: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 21: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 22: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 23: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 24: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 25: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 26: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 27: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 28: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 29: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 30: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 31: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 32: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 33: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 34: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 35: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 36: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 37: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 38: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 39: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 40: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 41: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 42: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 43: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 44: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 45: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 46: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 47: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 48: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 49: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 50: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 51: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 52: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 53: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 54: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 55: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 56: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 57: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 58: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 59: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 60: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 61: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 62: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 63: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 64: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 65: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 66: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 67: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 68: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 69: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 70: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 71: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 72: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 73: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 74: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 75: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 76: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 77: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 78: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 79: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 80: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 81: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 82: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 83: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 84: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 85: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 86: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 87: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 88: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 89: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 90: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Topic 91: Print Output
Description: Display a message to the console.
Python Example:
# Python
# Prints a string to the standard output
print("Hello, World!")
Shell Example:
# Shell
# Outputs a string to the terminal
echo "Hello, World!"
Comparison:
Both commands output text. Python uses the print() function, while Shell uses the echo command.
Topic 92: Declare Variables
Description: Define and print variables.
Python Example:
# Python
name = "Alice"
print("Name is:", name)
Shell Example:
# Shell
name="Alice"
echo "Name is: $name"
Comparison:
Python uses dynamic typing and print formatting. Shell uses variables with a `$` prefix.
Topic 93: If Condition
Description: Conditional execution based on a test.
Python Example:
# Python
x = 10
if x > 5:
print("Greater than 5")
Shell Example:
# Shell
x=10
if [ $x -gt 5 ]; then
echo "Greater than 5"
fi
Comparison:
Python uses `if` with colons and indentation. Shell uses `if [ ]` with `then` and `fi`.
Topic 94: For Loop
Description: Loop through a sequence of values.
Python Example:
# Python
for i in range(1, 4):
print("Iteration:", i)
Shell Example:
# Shell
for i in {1..3}; do
echo "Iteration: $i"
done
Comparison:
Python loops with `range(start, stop)`, Shell uses `{start..end}` or `seq`.
Topic 95: Reading a File
Description: Read and print file contents.
Python Example:
# Python
with open("data.txt") as f:
for line in f:
print(line.strip())
Shell Example:
# Shell
while IFS= read -r line; do
echo "$line"
done < data.txt
Comparison:
Python uses context managers and file iterators. Shell uses `while read` with redirection.
Topic 96: Writing a File
Description: Write text to a file.
Python Example:
# Python
with open("output.txt", "w") as f:
f.write("Hello World\n")
Shell Example:
# Shell
echo "Hello World" > output.txt
Comparison:
Python uses `open()` with `write()`. Shell redirects output using `>`.
Topic 97: Functions
Description: Create and call a reusable block of code.
Python Example:
# Python
def greet(name):
print(f"Hello, {name}")
greet("Bob")
Shell Example:
# Shell
greet() {
echo "Hello, $1"
}
greet "Bob"
Comparison:
Python functions use `def`, Shell defines functions with `()` and braces.
Topic 98: Command-line Arguments
Description: Accept arguments passed to the script.
Python Example:
# Python
import sys
print("Argument:", sys.argv[1])
Shell Example:
# Shell
echo "Argument: $1"
Comparison:
Python uses `sys.argv`, Shell uses `$1`, `$2`, etc.
Topic 99: Error Handling
Description: Gracefully handle execution errors.
Python Example:
# Python
try:
1/0
except ZeroDivisionError:
print("Error: Division by zero")
Shell Example:
# Shell
x=0
y=$((1 / x)) || echo "Error: Division by zero"
Comparison:
Python uses `try/except`. Shell uses logical OR (`||`) to catch errors.
Topic 100: Environment Variables
Description: Access and use environment variables.
Python Example:
# Python
import os
print("PATH:", os.getenv("PATH"))
Shell Example:
# Shell
echo "PATH: $PATH"
Comparison:
Python uses `os.getenv()`, Shell accesses variables with `$`.
Further Reading & References
1. Python Official Docs - https://docs.python.org/3/
2. GNU Bash Manual - https://www.gnu.org/software/bash/manual/
3. W3Schools Python - https://www.w3schools.com/python/
4. W3Schools Shell - https://www.w3schools.com/linux/linux_shell.asp
5. Real Python - https://realpython.com/
6. Learn Shell - https://www.learnshell.org/
7. GeeksForGeeks Python - https://www.geeksforgeeks.org/python-programming-language/
8. GeeksForGeeks Shell - https://www.geeksforgeeks.org/shell-scripting/
9. Linuxize Shell - https://linuxize.com/post/bash-scripting-tutorial/
10. Python TutorialsPoint - https://www.tutorialspoint.com/python/index.htm
11. Shell TutorialsPoint - https://www.tutorialspoint.com/unix/shell_scripting.htm
12. Python Cheatsheet - https://www.pythoncheatsheet.org/
13. Bash Guide for Beginners - https://tldp.org/LDP/Bash-Beginners-Guide/html/
14. Stack Overflow Python - https://stackoverflow.com/questions/tagged/python
15. Stack Overflow Bash - https://stackoverflow.com/questions/tagged/bash
16. Codecademy Python - https://www.codecademy.com/learn/learn-python-3
17. LinuxCommand.org - http://linuxcommand.org/
18. Automate the Boring Stuff (Python) - https://automatetheboringstuff.com/
19. Python on GitHub - https://github.com/python
20. Awesome Python - https://github.com/vinta/awesome-python
21. Awesome Shell - https://github.com/alebcay/awesome-shell
22. ShellCheck Linter - https://www.shellcheck.net/
23. PyPI - https://pypi.org/
24. Python Package Index - https://pypi.org/
25. Termux Shell Reference - https://wiki.termux.com/wiki/Shells
26. The Hitchhiker’s Guide to Python - https://docs.python-guide.org/
27. Advanced Bash-Scripting Guide - https://tldp.org/LDP/abs/html/
28. Linux From Scratch Shell - https://www.linuxfromscratch.org/
29. Python Exceptions Guide - https://docs.python.org/3/tutorial/errors.html
30. Shell Trap Guide - https://linuxhint.com/bash_trap_command/