Search code examples
vagrantvagrant-provision

Shell provision doesn't run sed command


I want to run 2 commands below to make terminal colorful. They work on terminal, but not in shell script.

bootstrap.sh:

#!/usr/bin/env bash

sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" ~/.bashrc
. ~/.bashrc  

If I append apt-get update at the end, only apt-get update is performed.


Solution

  • [Vagrant's shell provisioner][1] runs with privileged = true by default:

    privileged (boolean) - Specifies whether to execute the shell script as a privileged user or not (sudo). By default this is "true".

    When you perform vagrant ssh you login to a VM as vagrant user.

    That's why:

    sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" ~/.bashrc
    

    did changes in root's ~/.bashrc.

    Solution: execute sed to vagrant's home folder's .bashrc:

    sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" /home/vagrant/.bashrc
    

    Your final bootstrap.sh is:

    #!/usr/bin/env bash
    
    sed -i "s/#force_color_prompt=yes/force_color_prompt=yes/g" /home/vagrant/.bashrc
    . /home/vagrant/.bashrc