Below command is used to replace the password in a script but the perl command is adding a white space when it does the subsstitution

perl -i -p -e "s/^(password[]*=[ ]*).*$/\1 $passwd/" config.properties

cat config.properties
password= arche20

It does the work but it seems to be adding a space after password when it replaces. Could this be done without the space? Got a sed way:

sed -i "s/password.*/password=$passwd/g"
  • I got a sed way to do it .. but want to correct the perl way as well sed -i "s/password.*/password=$passwd/g"
    – Peck
    May 18, 2015 at 18:33
  • 1
    You can use that exact same command in perl. Why are you adding the [ ]? All you need is perl -i -pe "s/^(password *= *).*/\1$passwd/". Are the empty character classes supposed to be doing something?
    – terdon
    May 18, 2015 at 18:39

1 Answer 1


You have a space after \1 in your replacement, just remove that and you should be good

perl -i -p -e "s/^(password[]*=[ ]*).*$/\1$passwd/" config.properties
                                          Removed space here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.