An escaping of the variable within bash script

前端 未结 3 979
慢半拍i
慢半拍i 2021-01-27 09:43

My bash script writes an another bash script using printf.

printf \"#!/bin/bash
HOME=${server}
file=gromacs*
file_name=\\$(basename \"\\${file}\")
date=\\$(date          


        
3条回答
  •  逝去的感伤
    2021-01-27 10:12

    You have to escapes in printf with esscaps, e. g.:

    date=\$(date +"%%m_%%d_%%Y")
    

    should print date=\$(date +"%m_%d_%Y"). But you should avoid using printf, because you don't use it's capabilities. Instead you could cat the string to the file:

    cat > ${output}/collecter.sh <<'END'
    HOME=${server}
    ...
    done
    END
    

    This would allow you to avoid many escapes, and make the code more readable.

提交回复
热议问题