Bash Script

Bash scripting is a powerful tool for automating tasks in Unix-like operating systems. This tutorial will guide you through the basics of writing a simple Bash script.


1. What is a Bash Script?


A Bash script is a text file containing a series of commands that the Bash shell can execute. Bash (Bourne Again SHell) is the default command-line shell on most Linux distributions and macOS.


2. Prerequisites


Before you start, ensure you have:

- A Unix-like operating system (Linux or macOS).

- Basic knowledge of the command line.

- A text editor (e.g., `nano`, `vim`, `gedit`).


3. Creating Your First Bash Script


Step 1: Open Your Terminal

Open your terminal application.


Step 2: Create a New File

Use a text editor to create a new file. For example, using `nano`:

nano myscript.sh


Step 3: Add the Shebang Line

The shebang line tells the system which interpreter to use to execute the script. Add the following line at the top of your script:

#!/bin/bash


Step 4: Write Your Script

Below the shebang line, you can start writing your commands. Here’s a simple script that prints "Hello, World!" to the terminal:

#!/bin/bash

# This is a simple bash script

echo "Hello, World!"


Step 5: Save and Close the File

If you are using `nano`, press `CTRL + X`, then `Y` to confirm, and `Enter` to save the file.


4. Making Your Script Executable


Before you can run your script, you need to make it executable. Use the `chmod` command:

chmod +x myscript.sh


5. Running Your Script


Now you can run your script by typing:

./myscript.sh


6. Adding More Functionality


Variables

You can store values in variables and use them in your script.

#!/bin/bash

# Defining a variable

name="Alice"

echo "Hello, $name!"


User Input

Read input from the user using the `read` command.

#!/bin/bash

echo "Enter your name:"

read name

echo "Hello, $name!"


Conditional Statements

Use `if` statements to perform conditional logic.

#!/bin/bash

echo "Enter a number:"

read number

if [ $number -gt 10 ]; then

  echo "The number is greater than 10."

else

  echo "The number is 10 or less."

fi


Loops

Loops allow you to repeat commands.

#!/bin/bash

# A simple for loop

for i in {1..5}; do

  echo "Welcome $i times"

done


7. Putting It All Together


Here's a more complex script that combines variables, user input, conditional statements, and loops:

#!/bin/bash

# Script to greet a user and display a list of their favorite things


echo "Enter your name:"

read name

echo "Hello, $name!"


echo "How many favorite things do you want to list?"

read count


for ((i=1; i<=count; i++)); do

  echo "Enter favorite thing $i:"

  read thing

  echo "Adding $thing to your list."

done


echo "Here's your list of favorite things, $name:"

for ((i=1; i<=count; i++)); do

  echo "- Favorite thing $i"

done


8. Conclusion


You now have a basic understanding of how to create and run a Bash script. Bash scripting can automate a wide range of tasks and is a valuable skill for anyone working with Unix-like systems. Experiment with different commands and build more complex scripts as you become more comfortable with the basics.