echo a one-liner bash script with variables

10,716

Try this:

i="1"; echo -e '#!/usr/bin/env bash\nmyprogram -i '"\"input_${i}.txt\""

You can echo single- and double-quoted strings at the same time.

Share:
10,716
tflutre
Author by

tflutre

Updated on June 14, 2022

Comments

  • tflutre
    tflutre almost 2 years

    I would like to do that:

    i="1"; echo -e '#!/usr/bin/env bash\nmyprogram -i "input_${i}.txt"'
    

    and pipe it to a job scheduler.

    However, this doesn't replace the variable i by its value. Instead, I obtain this:

    #!/usr/bin/env bash
    myprogram -i "input_${i}.txt"
    

    I played a bit with option -e of echo and with single-/double-quote but could not make it work. For instance, I get this:

    i="1"; echo -e "#!/usr/bin/env bash\nmyprogram -i \"input_${i}.txt\""
    -bash: !/usr/bin/env: event not found
    

    My bash version is 4.1.2.