In Linux, you can define variables in a shell script or directly in the command line. Here’s how to do it:
In a Shell Script
-
Basic Variable Definition:
variable_name=valueExample:
my_var="Hello, World!" -
Accessing Variables:
To access the value of a variable, use the$symbol:echo $my_var -
Exporting Variables:
If you want to make a variable available to child processes, use theexportcommand:export my_var="Hello, World!"
In the Command Line
You can define and use variables directly in the terminal:
-
Define a Variable:
my_var="Hello, Terminal!" -
Use the Variable:
echo $my_var
Notes
- Variable names should not contain spaces and can include letters, numbers, and underscores.
- By convention, environment variable names are often written in uppercase letters.
This is how you can define and use variables in Linux.
