DEV Community

Trix Cyrus
Trix Cyrus

Posted on • Updated on

50 Essential Bash Script Commands Every Linux User Should Know(MUST READ)

Author: Trix Cyrus
Waymap Pentesting tool: Click Here
TrixSec Github: Click Here

Basic Commands for Bash Scripting

#!/bin/bash – Shebang to specify the script interpreter.
echo – Output text to the terminal.
read – Read input from the user.
if – Conditional statement to perform logic.
else – Alternative to if condition.
fi – End of an if block.
for – Loop to iterate over a list of items.
while – Loop until a condition is true.
do – Start a loop body.
done – End a loop.
case – Conditional to match patterns.
esac – End of a case block.
exit – Exit the script or loop.
rm – Remove files or directories in scripts.
chmod +x – Make a script executable.
sleep – Pause script execution for a specified time.
clear – Clear the terminal screen.
date – Display the current date and time.
pwd – Print the working directory.
Enter fullscreen mode Exit fullscreen mode

Intermediate Bash Script Commands

grep – Search for text patterns.
find – Locate files or directories.
awk – Pattern scanning and processing.
sed – Stream editor for filtering and transforming text.
cut – Remove sections from lines of text.
tr – Translate or delete characters.
df – Show disk usage in the script.
du – Display file space usage.
basename – Strip directory and suffix from filenames.
dirname – Extract directory path from a filename.
eval – Execute a string as a command.
trap – Catch signals and run cleanup code.
function – Define a function in Bash scripts.
$? – Check the exit status of the last command.
> – Redirect output to a file.
>> – Append output to a file.
2> – Redirect error output to a file.
&> – Redirect both standard and error output to a file.
source – Execute commands from a file within the current shell.
xargs – Build and execute commands from standard input.
Advanced Bash Script Commands
declare – Declare variables with specific attributes.
set – Change shell options and positional parameters.
getopts – Parse options in a script.
expr – Evaluate expressions.
let – Evaluate arithmetic expressions.
Enter fullscreen mode Exit fullscreen mode

Advanced arithmetic evaluation.

crontab – Schedule scripts to run at specific intervals.
rsync – Synchronize files between locations.
scp – Securely copy files over SSH within scripts.
ssh – Run remote commands via SSH in scripts.
Enter fullscreen mode Exit fullscreen mode

~Trixsec

Top comments (3)

Collapse
 
moopet profile image
Ben Sinclair

Hi, I'm not sure if you pasted this incorrectly from something you'd written in another editor?
Your lists are all rendered as code blocks and the section titled "advanced arithmetic evaluation" is a list of 4 completely unrelated commands.

touch – Create a new file within a script

touch isn't a command to create files, and it doesn't need to be run from inside a script!

Collapse
 
trixsec profile image
Trix Cyrus

Hi Ben, thanks for your feedback and for pointing that out! You're absolutely right – touch can be used to create a new file directly from the command line, not just within a script. I appreciate your clarification, and I’ll make the necessary updates to avoid any confusion.

Collapse
 
sonuhansda profile image
Sonu Kumar Hansda

That's great , very insightful !!