Skip to main content
3 of 7
deleted 2 characters in body
Jahid
  • 22.6k
  • 10
  • 97
  • 114

How to set a variable equal to the output from a command in Bash?

I am working on a simple scripting project for work that involves the use of Bash. I have a pretty simple script that is something like the following:

#!/bin/bash VAR1="$1" VAR2="$2" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF 

When I run this script from the command line and pass it the arguments, I am not getting any output. However, when I run the commands contained within the $MOREF variable, I am able to get output. I would like to know how one can take the results of a command that needs to be run within a script, save it to a variable, and then output that variable on the screen?

John
  • 25.5k
  • 5
  • 27
  • 26