Score:0

Bash sed command with sed itself as replacement, quotes not outputted

jp flag

I need to modify the content of a file, with the following sed command (which works fine):

sed -i '[email protected].*@'"date.timezone = $TZ"'@' $PHP_INI_DIR/conf.d/symfony.ini

This line should be injected in a script before the exec:

#!/bin/sh
set -e

exec "$@"

So i need another sed to say "replace before exec":

sed '/exec/i \sed -i '[email protected].*@'"date.timezone = $TZ"'@' $PHP_INI_DIR/conf.d/symfony.ini\n' docker-php-entrypoint

The problem are quotes which are not outputted:

#!/bin/sh
set -e

sed -i [email protected].*@"date.timezone = $TZ"@ $PHP_INI_DIR/conf.d/symfony.ini

exec

Putting a backslash \ before '[email protected] does nothing. Any suggestion?

Score:1
br flag

You can use double quotes within single quotes and vice versa to escape the quotes:

sed '/exec/i \sed -i '"'"'[email protected].*@"date.timezone = $TZ"@' $PHP_INI_DIR/conf.d/symfony.ini\n'"'"' docker-php-entrypoint

This way, the single quotes are escaped within the single quotes and the double quotes are used within the escaped single quotes.

user34295 avatar
jp flag
Thanks but it seems not working. When I issue the command, the shell seems to wait for another one. (doesn't print output)
br flag
It's possible that the command is not being properly terminated. You can try adding a backslash \ at the end of the sed command to indicate that the command continues on the next line: sed "/exec/i sed -i '[email protected].*@'"'date.timezone = \$TZ'"'@' $PHP_INI_DIR/conf.d/symfony.ini\\" docker-php-entrypoint
Score:1
in flag

This is a shell issue. The shell does "quote removal", so you need to study how quotes behave in the shell. Single quotes can't be nested nor escaped. You have to end the single quoted string to insert a single quote.

sed '/exec/i \sed -i '\''[email protected].*@'\''"date.timezone = $TZ"'\''@'\'' $PHP_INI_DIR/conf.d/symfony.ini\n'
user34295 avatar
jp flag
Same problem as @Salim answer, when I issue the command the shell doesn't ouput. Seems stuck waiting for termination.
in flag
You need to add the rest of the parameters (e.g. the file name).
user34295 avatar
jp flag
Sorry, you right, i noticed just after writing the comment.. thanks, i'm testing it!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.