How do variables work with echo?

QuestionsQuestions8 SkillsProDec, 12 2025
098

That's an excellent question! echo is indeed very powerful when combined with variables.

In the Linux terminal, you can define variables to store pieces of information. When you want to display the content of a variable, the echo command comes in handy.

Here's how it generally works:

  1. Define a variable: You assign a value to a name. For example:

    MY_MESSAGE="Hello from LabEx"

    (Notice there are no spaces around the = sign when assigning a value.)

  2. Echo the variable: You use echo followed by a dollar sign ($) and the variable's name. The $ tells the shell that you want to refer to the value of the variable, not the variable name itself.

    echo $MY_MESSAGE

When you run these two commands, the output will be:

Hello from LabEx

You can also combine text with variables:

echo "My custom message is: $MY_MESSAGE"

Output:

My custom message is: Hello from LabEx

This makes echo incredibly useful for displaying dynamic information or creating more descriptive outputs in your terminal sessions or scripts.

Feel free to try it out! Let me know if you'd like to try an example together.

0 Comments

no data
Be the first to share your comment!