-1

I'm running this command to find the mounted network folder :

df | awk '/[email protected]/ { 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 '/[email protected]/ { print $1 }'

How should I use the var in this case ?

2
  • Something like awk -v user=$USER '$1 ~ user {print $1}' will make the bash variable into an awk variable. Commented Nov 6, 2023 at 8:43
  • ... though you'd probably want to define a field separator to split username from mount, i.e. awk -F@ ... Commented Nov 6, 2023 at 9:04

1 Answer 1

0

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 /[email protected]/' { print $1 }'

To cope for the possibility, that the name stored on USER contains a space, you could alternatively write it as

df | awk "/[email protected]/"' { 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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.