Re: No escape from the dollar sign in SED
| Newsgroups | gmane.editors.sed.user |
|---|---|
| Message-ID | <CY1PR13MB048775180B6DC6CC436DF725B9BD0@CY1PR13MB0487.namprd13.prod.outlook.com> |
Hi, You could do the following also: cat <<-'EOF' | sed -e "s|[$]path|$usb|g" echo and here it is: ftpdir=$path echo more stuff EOF But you have to also ensure that "$usb" shell variable contents DONOT have anything which might be considered special by "sed" on it's right-hand-side of it's "s///" command, because otherwise this will cause sed to error out. For that you could do the following: usb_rhs=$(printf '%s\n' "$usb" | sed -e 's:[|\&]:\\&:g;$!s/$/\\/'); Now use this variable ($usb_rhs) in your "sed" command. It properly escapes the characters that are metacharacters to sed on the rhs of s/// command. ________________________________ From: [email protected] <[email protected]> on behalf of [email protected] [sed-users] <[email protected]> Sent: Tuesday, April 3, 2018 9:07 PM To: [email protected] Subject: No escape from the dollar sign in SED 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] [Non-text portions of this message have been removed] ------------------------------------ ------------------------------------ -- ------------------------------------ Yahoo Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/sed-users/ <*> Your email settings: Individual Email | Traditional <*> To change settings online go to: http://groups.yahoo.com/group/sed-users/join (Yahoo! ID required) <*> To change settings via email: [email protected] [email protected] <*> To unsubscribe from this group, send an email to: [email protected] <*> Your use of Yahoo Groups is subject to: https://info.yahoo.com/legal/us/yahoo/utos/terms/