How do I assign the result of eval to variable in bash script? How do I assign the result of eval to variable in bash script? bash bash

How do I assign the result of eval to variable in bash script?


pid=$( ps aux | grep `date +"%b"` | awk '{print $2}' )


You can cut out the need for grep since you are already using awk. The "$" does not need to be escaped because expansions do not occur inside of single quotes.

pid=$(ps aux | awk -vdate=$(date +%b) '$0 ~ date { print $2 }')

If you expect multiple pids to be returned, use an array:

pids=($(ps aux | awk -vdate=$(date +%b) '$0 ~ date { print $2 }'))for pid in "${pids[@]}"; do   ...done