How do I assign the result of an eval to a variable in a bash script?
I have a bash script that returns the list I want, but I need to put the results in a variable so that I can work with them one at a time in a for loop.
#!/bin/bash
processID="ps aux | grep `date +"%b"` | gawk '{print \$2}'"
eval $processID
How do I assign the result of eval to a variable? Thanks to
+3
source to share
2 answers
You can disable the need for grep since you are already using awk. "$" Don't need to be escaped because no extensions appear inside single quotes.
pid=$(ps aux | awk -vdate=$(date +%b) '$0 ~ date { print $2 }')
If you are expecting multiple pids to return, use an array:
pids=($(ps aux | awk -vdate=$(date +%b) '$0 ~ date { print $2 }'))
for pid in "${pids[@]}"; do
...
done
+1
source to share