How to Display Variable Value in Shell Script: Easy Guide
Displaying a variable value in a shell script is a fundamental skill that every command line user should master. This simple action allows you to verify data, debug issues, and build more transparent workflows directly in your terminal.
Whether you are writing complex automation or a quick one-liner, understanding how to output the contents of a variable ensures your scripts communicate their state clearly to the user. The good news is that the process is straightforward and relies on core shell features.
Using Echo for Basic Output
The most common method to view a variable is to use the echo command combined with a dollar sign and the variable name. This syntax tells the shell to retrieve the stored content and send it to the standard output stream for you to see.
This technique works consistently across major shells like Bash and Zsh, making it a portable solution for almost any scripting environment you encounter. It serves as the primary tool for logging values during development and production.
Simple Variable Expansion
To display the value, you simply type echo followed by the variable reference using the ${var_name} format. For instance, if you have a variable named greeting, running echo ${greeting} will print the text stored inside to the screen.
This expansion is immediate and dynamic, pulling the current value at the moment the command executes. It is the standard way to inspect data without modifying the original script logic or flow.
Combining Text and Variables
You can enhance readability by combining static text with the variable value inside the same echo statement. Wrapping the variable in double quotes allows the shell to concatenate the string and the content seamlessly.
For example, echo "The current user is ${username}" produces a full sentence that is easy for a human to read. This approach is highly recommended for creating user-friendly messages within your scripts.
Advanced Output Techniques
While echo is sufficient for most cases, the printf command offers superior control over formatting, especially when dealing with numbers, padding, or specific whitespace. It is the professional choice for polished output.
Learning to use printf ensures your data aligns correctly in columns or when you need to display raw escape characters like newlines and tabs. It provides the precision that basic echo commands lack.
Direct Reference and Indirection
Sometimes you might want to display a variable when you only know the name of that variable stored in another variable. This requires indirection, where you use the value of one to reference another.
By prefixing the referencing variable with a dollar sign, the shell performs two lookups: first to get the name, and then to get the actual value. This advanced trick is powerful for building dynamic and flexible scripts.
Displaying Exported Environment Variables
Environment variables, which are exported to child processes, follow the same display rules but are crucial for configuration management. You can check these using the same echo syntax to verify that paths or settings are correctly set.
Using env or printenv in conjunction with echo helps you audit the runtime environment of your scripts. This practice is vital for debugging failures that occur in different execution contexts.
Debugging with Set -x
If you prefer a broader view of script execution, the set -x command enables a trace mode that prints every command and variable expansion before it is executed.
This is an invaluable debugging tool because it shows you exactly how the shell interprets your code, including the hidden expansions. It helps you catch typos and logic errors instantly.
Targeted Debugging with Set +x
You can limit the noise by toggling the trace on and off using set +x around specific sections of code. This allows you to focus on the critical variable interactions without cluttering the output.
Combining explicit echo statements with trace debugging gives you a comprehensive toolkit for understanding variable flow. You gain fine-grained control over the visibility of your data.
Best Practices for Readability
Always quote your variable references, especially when the value might contain spaces or special characters. Using "${var}" prevents word splitting and ensures the value is treated as a single entity.
Consistent formatting and descriptive labels transform raw output into documentation. By structuring your display logic clearly, you make maintenance easier for yourself and your collaborators.
Experiment with these techniques to see which combination fits your workflow best, and you will find that displaying shell variables becomes an intuitive part of your daily scripting routine.