how to display variable value in shell script

How to Display Variable Value in Shell Script: Easy Guide

By Daniel Young
AD

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.

AD
an image of a computer screen with some type of text on the page and another webpage
an image of a computer screen with some type of text on the page and another webpage
an image of a computer screen with some text on it
an image of a computer screen with some text on it
How to Display Shells
How to Display Shells

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.

25 Beautiful Ways to Display Seashells in Your Home — Tidal Keepsakes
25 Beautiful Ways to Display Seashells in Your Home — Tidal Keepsakes
Tutorial: Bare-Metal Shell for Kinetis
Tutorial: Bare-Metal Shell for Kinetis
How to Use the Echo Command in Linux: A Detailed Guide
How to Use the Echo Command in Linux: A Detailed Guide

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.

AD

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.

different types of sea shells are shown on a piece of brown paper with words written below them
different types of sea shells are shown on a piece of brown paper with words written below them
sea shells and seashells are on display in glass cases with note attached to them
sea shells and seashells are on display in glass cases with note attached to them
How to Display Seashells Beautifully at Home
How to Display Seashells Beautifully at Home
an assortment of seashells displayed on a white board
an assortment of seashells displayed on a white board
an iphone screen showing different types of seashells
an iphone screen showing different types of seashells
the florida gulf coast shell identification poster
the florida gulf coast shell identification poster

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.

AD

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.

the different types of sea shells are displayed on a piece of brown paper with black spots
the different types of sea shells are displayed on a piece of brown paper with black spots
101 Ways to Display Shells
101 Ways to Display Shells
an image of seashells on display in a frame with the words coastal treasures
an image of seashells on display in a frame with the words coastal treasures
a framed display of sea shells and corals
a framed display of sea shells and corals
sea glass and driftwood are scattered on the beach
sea glass and driftwood are scattered on the beach
an image of seashells on display in a blue box with the words sea shells below it
an image of seashells on display in a blue box with the words sea shells below it
How to Find Big Seashells
How to Find Big Seashells
Shell Art
Shell Art
an old window is filled with seashells and sand in glass bottles that are hanging on the wall
an old window is filled with seashells and sand in glass bottles that are hanging on the wall
an old shell shop sign on top of a building
an old shell shop sign on top of a building
a poster showing the different types of seashells and how they are used to create them
a poster showing the different types of seashells and how they are used to create them
shells in a bowl with text overlay that reads 25 smart ways to style seashells
shells in a bowl with text overlay that reads 25 smart ways to style seashells
an assortment of seashells displayed on a gray surface with labels describing the names
an assortment of seashells displayed on a gray surface with labels describing the names
there are many sea shells on display in the wall next to some framed pictures and seashells
there are many sea shells on display in the wall next to some framed pictures and seashells

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.

AD

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.

AD