I’m running this command to find the mounted network folder :
df | awk '/andi@192.168.1.10/ { print $1 }'
This code is working fine but rather using ‘andi’ i want to pass it using ‘USER’ variable. It showing the correct value if I echo the USER var.
echo $USER
andi
But it’s not working when I used the var . Seems the single quot pair is not working with $VAR :
df | awk '/$USER@192.168.1.10/ { print $1 }'
How should I use the var in this case ?
>Solution :
As you found out, parameter expansion is not performed between single quotes. Hence $USER must be outside of single quotes, while $1 must be inside:
df | awk /$USER@192.168.1.10/' { print $1 }'
To cope for the possibility, that the name stored on USER contains a space, you could alternatively write it as
df | awk "/$USER@192.168.1.10/"' { print $1 }'
since parameter expansion is done within double quotes. Just make sure that awk sees the program as single parameter. Therefore we can’t have a space in front of the opening single quote.