Shell Custom Variables: Definition, Format, Types, and Usage
This article explains what shell variables are, their naming rules and assignment syntax, the different categories of variables, and demonstrates how to define and output custom variables in Linux shells with practical command‑line examples.
Variable definition: a variable is a storage space that can hold a mutable value, which can be changed in different environments. By default, each shell in Linux can be seen as a separate execution environment, so the same variable name may have different values in different shells.
1.1 Variable format
VariableName = VariableValue
Variable names cannot start with a digit or special character.
"=" is used for assignment.
Variable values can be numbers, strings, file paths, commands, or command results.
1.2 Classification of shell variables
Custom variables, environment variables, positional variables, predefined variables.
1.3 Variable output
a=1 echo $aecho outputs the variable; note the $ prefix before the variable name.
-----------------------------------------------------------------
2. Custom variables
Example 1:
[root@node1 ~]# Linux=7.2 [root@node1 ~]# echo $Linux 7.2 [root@node1 ~]# linux=6.5 [root@node1 ~]# echo $linux 6.5 [root@node1 ~]# echo $Linux 7.2Conclusion: there must be a space between echo and the variable, and variable values are case‑sensitive.
Example 2: invoking multiple variables
[root@node1 ~]# system=centos [root@node1 ~]# version=7.2 [root@node1 ~]# echo $system $version centos 7.2 [root@node1 ~]# echo sys{$Linux} sys{7.2} [root@node1 ~]# echo sys$Linux sys7.2Conclusion: you can directly echo the variable name.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
DevOps Cloud Academy
Exploring industry DevOps practices and technical expertise.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
