I always wondered how I could easily read several variables at once from some program I execute from bash. Usually I simply read all values to a single variable and do a barrel roll (for each) on it or I execute the program in question several times with slightly tweaked parameters. That’s all not so nice for performance critical scripts. So I usually try to read as much as possible with as less as possible program calls from within a script.

Well, this is a snipped I developed today that is much easier by simply making use of read and “<<<

read iUID iGID <<< `awk -v username="$USERNAME" -F ":" '$0 ~ "^"username":x:" {print $3" "$4}' /etc/passwd`

I’m also passing a variable down to awk used in a regular expression. Please notice that this does only work when $IFS is a single space (default).

if [[ $iUID =~ ^[0-9]+$ ]] && [[ $iGID =~ ^[0-9]+$ ]]; then

And here I can even validate both values easily as integers using a regular expression.

Bash is so damn powerful once you get the basics 🙂 Ah and yes I am aware that awk can do the integer check itself but I’m just not there.. yet 😉