How To Assign Output of a Linux Command to a Variable

When you run a command, it produces some kind of output: either the result of a program is suppose to produce or status/error messages of the program execution details. Sometimes, you may want to store the output of a command in a variable to be used in a later operation.

In this post, we will review the different ways of assigning the output of a shell command to a variable, specifically useful for shell scripting purpose.

To store the output of a command in a variable, you can use the shell command substitution feature in the forms below:

variable_name=$(command)
variable_name=$(command [option ...] arg1 arg2 ...)
OR
variable_name='command'
variable_name='command [option ...] arg1 arg2 ...'

Below are a few examples of using command substitution.

In this first example, we will store the value of who (which shows who is logged on the system) command in the variable CURRENT_USERS user:

$ CURRENT_USERS=$(who)

Then we can use the variable in a sentence displayed using the echo command like so:

$ echo -e "The following users are logged on the system:\n\n $CURRENT_USERS"

In the command above: the flag -e means interpret any escape sequences ( such as \n for newline) used. To avoid wasting time as well as memory, simply perform the command substitution within the echo command as follows:

$ echo -e "The following users are logged on the system:\n\n $(who)"
Shows Current Logged Users in Linux
Shows Current Logged Users in Linux

Next, to demonstrate the concept using the second form; we can store the total number of files in the current working directory in a variable called FILES and echo it later as follows:

$ FILES=`sudo find . -type f -print | wc -l`
$ echo "There are $FILES in the current working directory."
Show Number of Files in Directory
Show Number of Files in Directory

That’s it for now, in this article, we explained the methods of assigning the output of a shell command to a variable. You can add your thoughts to this post via the feedback section below.

If you read this far, tweet to the author to show them you care. Tweet a thanks
Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

10 thoughts on “How To Assign Output of a Linux Command to a Variable”

  1. You missed process substitution. For example: read uid gid < <(grep ^root /etc/passwd | awk -F: '{print $3 " " $4}') This allows you to set 2 or more variables if you mant.

    Reply
      • From the link Above “The backtick is also easily confused with a single quote.”

        This happened in this article for the first figure.

        ...
        OR
        variable_name='command'
        variable_name='command [option ...] arg1 arg2 ...'
        ...
        
        Reply
        • @Depressing

          True, it was a typo during publishing. We will modify the article as soon as possible. Thanks for the feedback.

          Reply

Got something to say? Join the discussion.

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.