Spaced input causing awk error

Hi all, Just want to say thanks for the great forum you have here, the old topics and posts have helped tremendously. So much so that I have managed to figure a lot out just by researching. However, I'm having a small issue that I simply can't find the answer to.

[root@hostname ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@hostname ~]# username=root
[root@hostname ~]# awk -v val=$username -F: '$1==val{print $3}' /etc/passwd
0

Works for me.

You should quote the $username variable:

user_id=`awk -v val="$username" -F: '$1==val{print $3}' /etc/passwd`
1 Like

it would have something to do with that but used single quotes instead of double. I'm such an idiot.

Yeap...the problem with using single quotes is that the shell will not interpolate the variables inside it.