Bash How to Save Output of a Command to a Variable

At times when writing a bash script you want to want to save the output of a command to a variable, in such case, you can use the following syntax.


Example 1: $() Syntax:

    vaiable_name = $(command)

    #!/bin/bash
    whoami_variable=$(whoami)
    echo $whoami_variable

    code2care

    Example - Save Command output to variable

Example 2: `` Syntax:

    vaiable_name = `command`

    #!/bin/bash
    whoami_variable=`whoami`
    echo $whoami_variable

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!