paint-brush
Cranking Up Your Terminal Game: Bash Scripting Tutorial – Part 2by@jiniuspark
533 reads
533 reads

Cranking Up Your Terminal Game: Bash Scripting Tutorial – Part 2

by Jin ParkMay 24th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Bash scripting provides a powerful set of tools for manipulating files on your system. From loops and functions to file handling, we'll cover it all in this tutorial. Comment generously to explain what Bash does and what it does not do. Keep your scripts as simple as possible to make scripting easier.
featured image - Cranking Up Your Terminal Game: Bash Scripting Tutorial – Part 2
Jin Park HackerNoon profile picture

Well hello again, script wizard! 🧙  It's refreshing to see you back for round two.


Judging by your return, I'll take a wild guess that the first installment of my Bash scripting tutorial sparked a fire in you. Get ready to stoke those flames as we dive deeper into the magic of Bash scripting.


You've already dipped your toes into the world of Bash scripting; now it's time to jump in headfirst. From loops and functions to file handling, we'll cover it all.


Just remember, practice makes perfect. So, let's roll up our sleeves and get scripting!

Advanced Bash Scripting Techniques

1. Looping Around in Bash

A significant part of scripting is making your code do the heavy lifting.

1.1 For Loops

A for loop in Bash allows you to repeat a series of commands a set number of times. Here's an example:


And one of the most effective ways to do this is by using loops. In Bash, we have for loops, while loops, and until loops.


for i in {1..5}
do
   echo "Welcome to loop iteration number $i!"
done


1.2 While Loops

A while loop keeps running as long as a condition is true. Here's how it looks:


count=1
while [ $count -le 5 ]
do
   echo "You're in loop iteration number $count!"
   count=$((count+1))
done


1.3 Until Loops

An until loop is the reverse of a while loop. It keeps running until a condition is true. Here's an example:


count=1
until [ $count -gt 5 ]
do
   echo "You're in loop iteration number $count!"
   count=$((count+1))
done


2. Defining Functions in Bash

In the world of Bash scripting, functions are our best friends. They help us avoid repeating the same code. Here's how to define a function:


greet() {
   echo "Hello, $1!"
}

greet "World"


In this script, greet is a function that takes one argument and prints a greeting.

3. File Handling in Bash

File handling is another crucial aspect of Bash scripting. With it, you can create, read, write, and delete files, among other things.

3.1 Creating Files

You can create a new file using the touch command:


touch newfile.txt


3.2 Reading Files

To read a file, you can use the cat command:


cat newfile.txt


3.3 Writing to Files

The echo command can be used to write to a file:


echo "Hello, World!" > newfile.txt


3.4 Deleting Files

To delete a file, use the rm command:


rm newfile.txt


What Can I Do With File Handling in Bash Scripting?

File handling in Bash scripting provides a powerful set of tools for manipulating files on your system. You can read, write, append, and delete files. Additionally, you can traverse directories and process multiple files at once, making file handling a key skill for any Bash scripter.

What Are Some Best Practices When Scripting in Bash?

There are several best practices for Bash scripting.


Here are a few:


  • Use clear and descriptive names for your scripts, variables, and functions.


  • Comment your code generously to explain what it does.


  • Keep your scripts as simple as possible. The easier they are to read, the easier they are to maintain and debug.


  • Make use of functions to avoid repeating code.


  • Always test your scripts thoroughly before using them in a live environment.


To round off the second part of our Bash scripting tutorial, we explored advanced techniques including loops, functions, and file handling. With these tools at your disposal, you're well on your way to becoming a Bash scripting guru.


However, remember that the key to mastery is practice. So, don't hesitate to use these techniques in your daily tasks and automate as much as possible. Happy scripting, and stay tuned for the next installment where we'll dive into even more advanced Bash scripting techniques!


echo "Happy scripting, until next time!"


Support me, and unleash your inner ‘Jinius’ with Jin Park - dive in now!

@https://ko-fi.com/jiniusme ❤️