Re: No escape from the dollar sign in SED

"Daniel Goldman [email protected] [sed-users]" <[email protected]> Tue, 3 Apr 2018 23:30:49 -0700
Newsgroups gmane.editors.sed.user
Message-ID <[email protected]>
Do you have to use $path as the literal string to replace? If yes, why? 
Could you use something simpler, like PATH_TO_REPLACE or similar?

If you have to use $path as literal string, just \ escape dollar sign in 
the sed script seen by sed, as already mentioned:

$ sed --version
sed (GNU sed) 4.2.2

$ cat /tmp/sed-1.sh
usb="/mnt/stick"
echo 'ftpdir=$path' | sed "s|\$path|$usb|"

$ /tmp/sed-1.sh
ftpdir=/mnt/stick

Daniel

On 4/3/2018 9:07 PM, [email protected] [sed-users] wrote:
>
> I was reading the excellent SED FAQ bud could not find an alternate solution to the following:
>
> The script below replaces the literal string $path with the contents of variable $usb
>
> The only way I could get this to work under busybox ash is with completely bare \\$
> to escape the literal $path in two separate chunks as \\$'path' which is REALLY weird.
>
> root@zero:~# usb='/mnt/stick'; echo 'Path $path is it!' | sed -r 's|'\\$'path'"|$usb|g"
> Path /mnt/stick is it!
>
> It's great that it works because it allows me to pre-process my here-block before I
> write it out to a file, but jeepers it would be nice to understand what's happening
> and whether there are any other ways to do this under busybox with ash.
>
> Here is the slightly simplified here-doc version which I actually use:
>
> usb='/mnt/stick'
> cat <<-'EOF' | sed -r 's|'\\$"path|$usb|g"
> echo and here it is:
> ftpdir=$path
> echo more stuff
> EOF
>
> Best regards
> Stepan Novotny
>
>
>
>
>
> [Non-text portions of this message have been removed]
>
>
>
> ------------------------------------
> Posted by: [email protected]
> ------------------------------------
>