BASH Tutorial



Introduction to Bash


๐Ÿ’ป Introduction to Bash

Bash stands for Bourne Again SHell. It's one of the most widely used shell environments for Linux and Unix systems. Bash is a command-line interface that allows users to interact with the operating system and automate tasks using shell scripts.

๐Ÿ”” Did You Know?

Most Linux distributions use Bash as the default shell, and even macOS uses a version of Bash in its Terminal.

๐Ÿ› ๏ธ What Can Bash Do?

  • Run Linux commands interactively or from a file
  • Automate system tasks (backups, file management, etc.)
  • Use variables and control structures (if, loops)
  • Chain commands using pipes, redirection, and logical operators

๐Ÿ“ฆ Sample Bash Commands

# Print text to screen
echo "Hello, world!"

# List files and directories
ls -l

# Change directory
cd /home/user

# Make a new directory
mkdir my_folder

# Remove a file
rm myfile.txt
  

๐Ÿ“„ Writing a Bash Script

Save your commands in a .sh file and make it executable:

#!/bin/bash

echo "Starting backup..."
cp -r /home/user/documents /home/user/backup
echo "Backup completed!"
  

Make it executable and run:

chmod +x backup.sh
./backup.sh

๐Ÿ” Control Structures

Bash allows basic logic like conditions and loops:

#!/bin/bash

# If condition
if [ -f "myfile.txt" ]; then
  echo "File exists."
else
  echo "File not found."
fi

# Loop
for i in {1..5}; do
  echo "Number $i"
done
  

๐Ÿ“Œ Real-World Uses

  • ๐Ÿ”„ Automate system backups
  • ๐Ÿงช Setup development environments
  • ๐Ÿ“ค Deploy web servers or services
  • ๐Ÿ“ Manage large file systems quickly

๐Ÿ’ก Pro Tip:

Use #!/bin/bash as the first line in any script to ensure it runs in the Bash environment. You can also debug using bash -x script.sh to see each line as it runs.

๐ŸŽฏ Conclusion

Bash is powerful for both beginners and professionals. Once you get the hang of writing scripts, you can automate virtually anything in a Linux system. Whether you're managing files or configuring servers, Bash makes your life easier.


๐ŸŒŸ Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review