Exploring Bash — Part 2

Dineshkumaar R
5 min readApr 7, 2024

--

Introduction

Hey Friends, Welcome back to my Bash exploration blog series! Stay tuned for insights and tips to become a Bash scripting ninja. Let’s dive in!

Declaring Variables

When working with Bash scripting, declaring variables is essential for storing and manipulating data. In Bash, variables are declared using the syntax ‘variable_name=value’. It’s crucial to note that there shouldn’t be any spaces around the ‘=’ sign.

Valid variable names in Bash can consist of letters (both uppercase and lowercase), numbers, and underscores. However, the variable name must start with a letter or an underscore. It’s also a good practice to use descriptive names to make your code more readable.

Using Variables

Once you’ve declared variables, you can access their values using the ‘$’ symbol followed by the variable name. This allows you to utilize variables within commands, strings, and arithmetic operations.

name="Dinesh"
echo "Hello, $name!" # Output: Hello, Dinesh!

Variables can also be used in arithmetic operations:

x=5
y=16
echo $((x + y)) # Output: 21

Environment Variables

Environment variables play a crucial role in Bash scripting, as they provide information about the environment in which the script is running. These variables are set by the system and can be accessed by Bash scripts.

Some commonly used environment variables include:
‘PATH’: Specifies directories where executable programs are located.
‘HOME’: Points to the user’s home directory.
‘USER’: Represents the username of the current user.

echo $PATH
echo $HOME
echo $USER

Understanding and utilizing environment variables is essential for writing portable and flexible Bash scripts.

Variable Scope

Variable scope refers to the visibility and accessibility of variables within a Bash script. In Bash, variables can have either local or global scope.

Local variables are defined within a specific function or script block and are only accessible within that scope. Global variables, on the other hand, are defined outside of any function and can be accessed from anywhere within the script.

Managing variable scope is important to avoid conflicts and unintended side effects in your Bash scripts. It’s generally recommended to use local variables whenever possible to minimize the risk of unintentional variable modification.

By understanding variable scope and following best practices, you can write more maintainable and robust Bash scripts.

#!/bin/bash

# Global variable
global_var="Global"

# Function definition
function example_function {
# Local variable
local local_var="Local"

# Accessing local and global variables within the function
echo "Inside function:"
echo "Local variable: $local_var"
echo "Global variable: $global_var"
}

# Accessing local and global variables outside the function
echo "Outside function:"
echo "Global variable: $global_var"
echo "Local variable: $local_var" # This will produce an error because local_var is not defined in the global scope

Displaying Output

In Bash scripting, the ‘echo’ command is used to display output to the terminal. It’s a fundamental command for communicating information to the user or for debugging purposes. The basic syntax of the `echo` command is simply ‘echo <message>’, where ‘<message>’ is the text you want to display.

Additionally, ‘echo’ supports formatting options such as the ‘-e’ flag for interpreting backslash escapes, allowing for more control over the output format.

echo "Hello, world! "

echo -e "Name:\tDinesh\nAge:\t25"

Reading Input

The ‘read’ command is used to read input from the user in Bash scripts. It prompts the user to enter data and then stores the input into one or more variables. The basic syntax for ‘read’ is ‘read <variable>’, where ‘<variable>’ is the name of the variable to store the input.

echo "Enter your name:"
read name
echo "Hello, $name!"

In this example, the user is prompted to enter their name, which is then stored in the variable `name` and displayed back to them.

Command Output

Command substitution, denoted by ‘$(…)’, allows you to capture the output of commands and store it into a variable. This is particularly useful when you need to use the output of one command as input to another command or for further processing.

files=$(ls)
echo "Files in current directory: $files"

In this example, the ‘ls’ command lists the files in the current directory, and its output is captured and stored in the variable ‘files’, which is then echoed back to the user.

Understanding how to effectively display output, read input, and capture command output is essential for creating interactive and informative Bash scripts. These functionalities enable interaction with users and facilitate data processing within scripts.

Summary of the blog

1. Declaring Variables: Bash variables are declared using ‘variable_name=value’ syntax without spaces around ‘=’.
2. Environment Variables: Crucial system information is stored in environment variables like ‘PATH’, ‘HOME’, and ‘USER’.
3. Variable Scope: Variables can have local or global scope in Bash, influencing their accessibility and potential conflicts.
4. Displaying Output: ‘echo’ command prints output to the terminal, supporting formatting options like ‘-e’.
5. Reading Input: ‘read’ command prompts users for input, storing it into variables for further script use.
6. Command Output: Command substitution ‘$(…)’ captures output from commands, facilitating data processing.

URL’s :

  1. Exploring bash — Part1 : https://medium.com/@dineshkumaar478/exploring-bash-part-1-3dabf4b5ef9e

Thank you for taking the time to read my blog. Wishing you a joyful learning experience ahead!

--

--

Dineshkumaar R

🔐 Cyber Security Engineer | Penetration Tester | VAPT Specialist | 🚩 Hackthebox (Pro Hacker) | 🏆 Tryhackme (Guru)