Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
[Link]
Shell Scripting Cheat Sheet
Akshat Jain
4–5 minutes
3 min read
Feb 14, 2026
Shell scripting is a way to automate tasks by writing scripts
(sequences of commands) in a text file, which the shell can
execute.
Press enter or click to view image in full size
1 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
Shell Scripting Cheat Sheet
Basic Shell Scripting Commands:
1. #!/bin/bash
• Purpose: This is called the shebang line. It specifies the
interpreter for the script (in this case, Bash). The script will be
executed by the Bash shell.
• Example: This script will print “Hello, World!” to the screen.
#!/bin/bash echo "Hello, World!"
2. echo
2 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
• Purpose: The echo command is used to print a message or the
value of a variable to the terminal.
• Usage :Example:
• echo "Message" – Prints a string to the terminal.
• echo $variable – Prints the value stored in a variable.
echo "Hello, World!"
name="Alice"
echo "Hello, $name"
3. Variables
• Purpose: Variables are used to store data that can be reused
later in the script.
• Usage: Example:
• variable_name="value" – Declare and assign a value to a
variable.
• echo $variable_name – Access the value of a variable.
name="Alice"
echo "Hello, $name"
3 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
4. Conditionals (if/else)
• Purpose: Conditional statements allow the execution of
commands based on whether a condition is true or false.
• Usage:
if [ condition ]; then
else
fi
number=5
if [ "$number" -eq 5 ]; then
echo "The number is 5 buddy"
else
echo "Teri lowwde"
fi
• Example:
if [ $name == "Alice" ]; then
echo "Hello, Alice!"
else
4 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
echo "You're not Alice."
fi
• This checks if the variable name is equal to "Alice" and prints a
message accordingly.
5. Loops (for/while)
• Purpose: Loops allow repeated execution of commands. Useful
for iterating over items or performing repetitive tasks.
• For Loop:
for i in 1 2 3; do
echo "Loop $i"
done
• This will loop through the numbers 1, 2, and 3, printing each
one.
• While Loop:
count=1
while [ $count -le 5 ]; do
echo "Counter: $count"
((count++))
5 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
done
• This will increment count from 1 to 5 and print the value of
count each time.
6. Reading Input
• Purpose: Accept user input during the execution of a script.
• Usage:
Get Akshat Jain’s stories in your inbox
Join Medium for free to get updates
from this writer.
Remember me for faster sign in
read variable_name
echo "You entered: $variable_name"
• Example:
echo "Enter your name:"
read name
echo "Hello, $name!"
6 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
7. Functions
• Purpose: Functions allow you to group related commands
together and reuse them within your script.
• Usage:
my_function() {
echo "This is a function"
}
my_function
• Example:
greet() {
echo "Hello, $1!"
}
greet "Alice"
• $1 refers to the first argument passed to the function.
8. Making a Script Executable
• Purpose: Allows you to run a script as if it were a command
from the terminal.
7 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
• Steps:
Make the script executable using chmod:
chmod +x script_name.sh
Run the script by calling it directly:
./script_name.sh
• This allows you to execute the script without explicitly invoking
the interpreter (e.g., bash script_name.sh).
9. Comments
• Purpose: Comments are used to explain parts of your script
and make it easier to understand.
• Usage:
• Example:
# This script greets the user echo "Hello, User!"
10. File Redirection and Pipes
• Purpose: Shell scripting allows you to redirect input/output or
pipe the output of one command to another.
8 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
• Redirection:
• > – Redirects output to a file (overwrites).
• >> – Redirects output to a file (appends).
• Pipes:
• | – Pipes the output of one command to another.
• Examples:
echo "Hello, World!" > [Link]
cat [Link]
ls | grep "file"
11. Exit Status
• Purpose: Every command in a script returns an exit status
code, which indicates whether the command was successful.
• 0 – Success
• Non-zero — Error
• Usage:
if [ $? -eq 0 ]; then
echo "Last command was successful."
9 of 10 6/22/26, 21:30
Shell Scripting Cheat Sheet . Shell Scripting Cheat Sheet | by Akshat Jain | Medium about:reader?url=https%3A%2F%[Link]%2Fshell-scripting-chea...
else
echo "Last command failed."
fi
10 of 10 6/22/26, 21:30