Get the Output of a Program in a Variable in Bash

Posted on August 11, 2019 at 11:52 am

Do you need to get the output of a program in a variable in Bash?

There are two simple ways to achieve that:

var=`./run.sh params`

Or alternatively you can use $(), i.e:

var=$(./run.sh params)

Remember to not put spaces before or after the = operator.

If you need to capture also error messages use 2>&1:

var=$(./run.sh params 2>&1)

Receive updates via email

Other Posts

Updated Posts