0

I would like to change the bash command prompt string from

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

to

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '

Specifically, it is changing the colour from green to red, by change 32 to 31.

I would like to change the file rather than deploy/copy in a full file.

I would like to search through the file, and change the line if it matches either scenario.

I tried escaping all of the special characters, but the YAML regex complained about the \ and various characters.

David
  • 313
  • 2
  • 13

2 Answers2

3

Even if this ticket is over two years old, I found me in the same situation.

I tried a bit and found a solution to this challenge

  - name: Set default color in bash
lineinfile:
  path: /home/example-user/.bashrc
  regexp: "^    PS1='\\${debian_chroot:\\+\\(\\$debian_chroot\\)}\\\\\\[\\\\033\\[01;32m\\\\\\]\\\\u@\\\\h\\\\\\[\\\\033\\[00m\\\\\\]:\\\\\\[\\\\033\\[01;34m\\\\\\]\\\\w\\\\\\[\\\\033\\[00m\\\\\\]\\\\\\$ '"
  line: "    PS1='${debian_chroot:+($debian_chroot)}\\[\\033[01;32m\\]\\u\\[\\033[01;32m\\]@\\h\\[\\033[00m\\]:\\[\\033[01;34m\\]\\w\\[\\033[00m\\]\\$ '"
  backup: yes
when: ansible_os_family == "Debian"

Hope this might help others too.

Mrk
  • 31
  • 2
2

Why not just set PS1 to desired value?

- lineinfile:
    create: yes
    mode: 0600
    dest: /root/.bash_aliases
    owner: root
    regexp: '^PS1='
    line: 'PS1="${debian_chroot:+($debian_chroot)}\[\033[01;31m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ "'
    state: present

I used single quotes as outer ones as it doesn't require escaping characters.

Konstantin Suvorov
  • 3,836
  • 1
  • 11
  • 13
  • 1
    There are other lines in the file is with PS1=, or that even start with PS1=. The substring that is unique is `PS1='${debian_chroot:+($debian_chroot)}\[` – David Jun 07 '17 at 15:38