0
0
Bash Scriptingscripting~5 mins

Variable assignment (no spaces around =) in Bash Scripting

Choose your learning style9 modes available
Introduction

We use variable assignment to store values in a name. This helps us reuse data easily in scripts.

When you want to save a user's input to use later.
When you need to keep a count or number that changes.
When you want to store a file name or path to use multiple times.
When you want to hold a command result to use in other commands.
When you want to set configuration options inside your script.
Syntax
Bash Scripting
variable_name=value

Do NOT put spaces around the = sign.

Variable names should start with a letter or underscore, and contain letters, numbers, or underscores.

Examples
Assigns the word John to the variable name.
Bash Scripting
name=John
Stores the number 10 in the variable count.
Bash Scripting
count=10
Saves a file path string in the variable path.
Bash Scripting
path=/home/user/docs
Assigns a string with spaces by using quotes.
Bash Scripting
greeting="Hello World"
Sample Program

This script assigns the name Alice to the variable name. Then it prints a greeting using that variable.

Bash Scripting
#!/bin/bash

# Assign a value to a variable
name=Alice

# Use the variable
echo "Hello, $name!"
OutputSuccess
Important Notes

Remember: No spaces around the = sign. name = Alice will cause an error.

Use quotes if the value has spaces or special characters.

To use the variable value, prefix it with $, like $name.

Summary

Variable assignment stores data for reuse.

No spaces allowed around = in bash variable assignment.

Use $ to access the variable's value.