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

awk: using external variables from bash

I am using awk inside of the bash script to print a new file contained the name of the variable defined in bash

#!/bin/bash
file_name='test.log'
awk -v file="$file_name"  '
    BEGIN {
        print "@ subtitle \"file\""
       }

in that case the awk prints

@ subtitle "file"

instead of the expected output

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

@ subtitle "test.log"

>Solution :

You cannot use a variable in a string as-is but you can concatenate between a string and a variable like so:

print "@ subtitle " file ""

In this case, the empty string "" is useless, but it is just as an example. So you can just get rid of it and it will behave same with/without it.

print "@ subtitle " file

In your case, you want something like this:

print "@ subtitle \"" file "\""
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