I am trying to append create a string that will be evaluated in the middle of a grep line. It is to find possible occurrences of different usernames within a certain file. However, I do not know how many usernames there will be. So, I cycle through the array containing the list of usernames and return at output like so "-e username -e username2 -e username3" that will be used in the grep line.
However, this small piece of code seems to be giving me trouble. I've tried several approaches, but UNIX still believes I am trying to pass some sort of command:
eGrepUsernames(){
returnString="";
for username in "${usernames[@]}"
do
$returnString="$returnString -e $username" #error given, command not found
done
echo returnString;
}
Later, I call the function over here:
declare -a activeUsers=$( who | grep `eGrepUsernames` )
What am I doing wrong in this case?
$
operator is the operator to expand a variable to its value. It's not a variable access operator so much as an "insert variable contents here" operator.