How to define variables in bash?

QuestionsQuestions8 SkillsProYour First Linux LabNov, 02 2025
074

In Bash, you can define variables by simply assigning a value to a variable name without any spaces around the equal sign (=). Here’s the syntax:

variable_name=value

Example:

  1. Defining a String Variable:
greeting="Hello, World!"
  1. Defining an Integer Variable:
count=10
  1. Defining a Variable with Spaces:

If you want to include spaces in the value, you should enclose the value in quotes:

message="This is a message with spaces."

Accessing Variables:

To access the value of a variable, use the dollar sign ($) before the variable name:

echo $greeting

This will output:

Hello, World!

Note:

  • Variable names should not start with a number and can contain letters, numbers, and underscores.
  • By convention, variable names are usually written in uppercase letters.

0 Comments

no data
Be the first to share your comment!