Part of the Korn script I am writing requires that I replace all occurrences of the ' character with two occurrences (''). I am trying to log some SQL that I am generating in this script to a column in another table but single quotes need to be replaced with 2 instances of the single quote character. I know there must be examples of this functionality somewhere but I'm not finding a string replace example specific to variables anywhere.
|
|
||||
|
|
|
Is there any particular reason you couldn't call This simple script:
gives me this output:
|
|||||||
|
|
In ksh93 and zsh, there's a string replacement construct
This construct is also available in bash, but the quoting rules are different. The following snippet works in all of ksh93, bash and zsh.
In ksh88 and other shells, you need to write a relatively complex loop to replace the single quotes one at a time. The following snippet doubles single quotes, but leaves newlines unchanged).
Alternatively, you can use sed. Be careful when feeding the data to sed, as
If there are no newlines in the string, the following much simpler sed command suffices.
|
|||||
|