3

I have the following template, which I'm writing to a config file:

template = """mkdir /root/.ssh \
        && chmod go-rwx /root/.ssh \
        && apt-get install -y \
            gcc \
            libssl-dev \
            python-pip \
            openssh-server \
PATH=/usr/local/jdk1.8.0/bin:$PATH 
"""
with open('config', 'w') as f:
    f.write(template)

I'm getting the following format:

mkdir /root/.ssh         && chmod go-rwx /root/.ssh         && apt-get install -y             gcc             libssl-            dev             python-pip             openssh-server

instead of:

mkdir /root/.ssh \
            && chmod go-rwx /root/.ssh \
            && apt-get install -y \
                gcc \
                libssl-dev \
                python-pip \
                openssh-server \
PATH=/usr/local/jdk1.8.0/bin:$PATH 

How can I format my string template to the correct format?

cybertextron
  • 10,547
  • 28
  • 104
  • 208

1 Answers1

8

In Python \ is an escape character, you have to escape it with \\:

template = """mkdir /root/.ssh \\
        && chmod go-rwx /root/.ssh \\
        && apt-get install -y \\
            gcc \\
            libssl-dev \\
            python-pip \\
            openssh-server \\
PATH=/usr/local/jdk1.8.0/bin:$PATH 
"""
with open('config', 'w') as f:
    f.write(template)
Daniel
  • 42,087
  • 4
  • 55
  • 81