Saving awk output to variable

Jeremy picture Jeremy · Sep 6, 2013 · Viewed 117.2k times · Source

Can anyone help me out with this problem?

I'm trying to save the awk output into a variable.

variable = `ps -ef | grep "port 10 -" | grep -v "grep port 10 -"| awk '{printf "%s", $12}'`
printf "$variable"

EDIT: $12 corresponds to a parameter running on that process.

Thanks!

Answer

wens picture wens · Sep 6, 2013
#!/bin/bash

variable=`ps -ef | grep "port 10 -" | grep -v "grep port 10 -" | awk '{printf $12}'`
echo $variable

Notice that there's no space after the equal sign.

You can also use $() which allows nesting and is readable.