Search code examples
bashshellquoting

unterminated `s' command in sed


The command below is being used in a simple bash script to get the temporary password in MySQL 5.7.

vzctl exec $VEID 'defaultpass=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n 's/.*root@localhost: //p')'

However it says the following when I run the command for testing:

sed: -e expression #1, char 19: unterminated `s' command

Can someone please explain me what I am doing wrong exactly. I am by no means an expert, but I thought this would be correct. I also tried replacing ' with ", but that didn't work. No errors then, but $defaultpass returns empty then.

Follow-up: unfortunately when I use double-quotes,as described by pii_ke, the output of the variable is empty / blank. So that's not working. Maybe something else is wrong here?


Solution

  • Since the whole third argument to vzctl command is quoted with single quotes. You should place sed commands inside double quotes:

    vzctl exec $VEID 'defaultpass=$(cat /var/log/mysqld.log | grep "A temporary password is generated for" | tail -1 | sed -n "s/.*root@localhost: //p")'