In a Bash Script How Do You Assign a Variable a Value of a Complex Linux Expression?

Problem scenario:  You know how to assign variables in a Bash script. But how do you assign the output of a complex command such as this?

cat foo.txt | grep continual | grep integration | uniq

To push the output of some compound statement into a text file is simple.  Sometimes you want to redirect everything to a variable. How do you do this?

Solution:  Use $(expression)

For example:
varA=$(cat foo.txt | grep continual | grep integration | uniq)

Remember that there cannot be spaces around the equal sign.  Later call varA like this to evaluate it for its content: $varA

Leave a comment

Your email address will not be published. Required fields are marked *