1. Overview
In Linux shell scripting, echoing the value of a variable is a common task. However, there may be situations when we need to display the variable name itself rather than the value. This is useful for debugging or logging as it provides insights into the program’s flow and helps us identify data sources reflexively.
In this tutorial, we’ll explore how we can output a variable name instead of the variable value in Linux.
The commands used in this tutorial were tested in the Bash shell version 5.1.16.
2. echo With Strings
Before diving into advanced methods, let’s revisit the basic echo command in Linux scripting. In short, echo is used to output its argument to the terminal.
For example, let’s print out Linux:
$ echo Linux
Linux
Here, we used the echo command to output the unquoted argument Linux we passed to it. In this case, the command sees that argument as a regular string.
3. Variable Names and Values
Let’s review how we can assign a value to variables:
$ varName=Linux
Here, varName is the variable name, and Linux is the variable value. Now, the varName variable holds the string Linux.
Now, let’s display varName‘s value via echo:
$ echo $varName
Linux
Notably, the syntax changed when we wanted to display a variable value as opposed to assign it. In summary, by adding the $ sign as a prefix to the variable name, we interpolated the variable.
4. Using Parameter Expansion
One of the features of Bash is the ability to use parameter expansion to manipulate and transform variables. Basically, parameter expansion is a process that the shell performs before executing a command whereby it replaces a parameter (variable, special character combination) with its respective value or an alternative to it, depending on the modifier.
One parameter expansion form is ${!prefix@}, where prefix can be changed according to our needs. Generally, the ${!prefix@} syntax expands to the names of variables whose names begin with prefix, separated by the first character of the IFS special variable. As the internal field separator, the special IFS variable defines the characters used to split a string into words. By default, IFS contains a space, a tab, and a newline.
For instance, let’s suppose we have the following variables:
$ name="Muiz"
$ age=22
$ city="New York"
$ country="USA"
Now, let’s get the names of all the variables that start with c:
$ echo ${!c@}
city country
The output is two words: city and country
Further, the output is separated by a space, which is the first character of IFS.
Additionally, we can display the variable that has the name we want to display by expanding prefix accordingly:
$ echo ${!city@}
city
We can see that the output is the name of the variable itself. However, if there were another variable that starts with city, it would also have been part of the output.
5. Using ${!prefix@} With Double Quotes
When ${!prefix@} is used, and the expansion appears within double quotes, each variable name expands to a separate word. Thus, this means that the shell will treat each name as an individual argument. For example, let’s assign the names of all the variables that start with c to an array:
$ names=("${!c@}")
$ echo ${names[0]}
city
$ echo ${names[1]}
country
The $names array has two elements: city and country
Each element corresponds to a variable name.
However, if we omit the double quotes, the shell will treat the expansion as a single word:
$ array=(${!c@})
$ echo ${array[0]}
city country
$ echo ${array[1]}
From the output above, we can see that the array has only one element, city country, which is a concatenation of the variable names.
While this is regular behavior for array assignments, it’s important for variable name interpolation since arrays often play a part in that.
6. Escaping the $ Dollar Sign
Another way to only display the variable name is by using *\* (backslash) to escape the $ dollar sign:
$ echo \$age
$age
Here, we simply disable the special function of the dollar sign via the escape. This way, we end up with the variable name preceded by $.
Although it’s a static change and not dynamically usable, this may come in handy for easier code editing. Further, by automating the process via search and replace, we can switch between variables and variable names as needed.
In fact, this is a clean way to show a variable with its dollar sign prefix.
7. Displaying Variable Name With awk
The awk command is a powerful tool for processing and manipulating text files or streams. We can use awk to extract information from text streams and apply patterns and actions to the matching lines. Let’s see how awk works in our case.
For instance, let’s suppose we have a variable, country:
$ country="USA"
As we already saw, we display the value of country with the echo command:
$ echo "$country"
USA
Notably, echo “$country” displays the value of country because, in Bash, double quotes enable variable expansion in the shell.
However, we can print out a variable name along with $ while using single quotes:
$ echo '$country'
$country
Now, we’re able to print out $country because single quotes disable expansions.
Now, let’s use awk to remove the $ from the front so we can print out the variable name only:
$ echo '$country' | awk '{gsub(/\$/,"")}1'
country
In the code snippet above, there are two commands (echo and awk) in a pipeline. The pipe symbol | means that the output of the first command should be passed to the second command as an input. In particular, awk gets its input from the output of echo, namely – $country.
Next, the awk script within single quotes and performs two actions:
- gsub(/\$/,””) replaces all instances of $ (escaped due to its special meaning in regular expressions) with an “” empty string
- 1 is a shorthand for printing the line after the modifications
Thus, we get the variable name only. Of course, this simplified method is prone to errors when the dollar sign isn’t part of a variable name, but the idea remains the same.
8. Conclusion
In this article, we learned how to output a variable name instead of the variable value in Linux.
First, we reviewed the basic echo command and how to assign and display variables. Next, we used the parameter expansion form ${!prefix@} to get the names of variables that begin with a certain prefix. We also learned how to escape the $ sign to show a variable directly.
Finally, we saw how we can use the awk command to manipulate and remove the $ sign from the variable name.
In conclusion, echoing variable names can be helpful and fairly straightforward.