Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

passing shell variable into parameter in shell script

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading