Welcome to the most active Linux Forum on the web.
Go Back > Blogs > linux-related notes
User Name


Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
Rate this Entry

The "sed" command doesn't need quotes

Posted 09-15-2017 at 02:20 PM by the dsc
Updated 12-01-2017 at 11:24 PM by the dsc

You may have some trouble when trying to make sed replacements along the lines of:

sed 's|$originalpath|$otherpath/$filename|'

If the variables contain things like the "-" character, or some other special characters, some of the sed commands may be terminated before they should, in error, as it's taking part of the variable as sed commands, rather than parameters. Something like that. Then the variables would ideally be quoted, but with the outermost quotes, it's perhaps impossible or kind of confusing/insane.

But you can use something like:

sed s}"$originalpath"}"$otherpath"/"$filename"}

No need for the outer quotes!

The double quotes are needed for the strings to be understood as strings, and their content used, rather than literally their names, I guess.

"}" is just what I though is perhaps the most unlikely character to be in a file or directory name.

EDIT: It's perhaps not the best choice as the ideal string usage is "${STRING}" even if you're not using it to the fullest extent, like an indexed array. Using that way will preserve multiple spaces in strings, which would otherwise be converted to a single space, messing up with stuff like folders or files that for some odd and arguable reason have multiple spaces. Maybe a question mark would be preferable, it's not just as unlikely in a file name, but rather uncommon, I suppose.

But the "pipe" character as a replacement for the "correct" slash no longer works, as it's interpreted as a pipe. But "plus" works without that problem, and without all the crazy escaping of slashes

As always, it may have some dangerous implications, either technical or political.
Posted in Uncategorized
Views 303 Comments 0
« Prev     Main     Next »
Total Comments 0




All times are GMT -5. The time now is 10:32 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration