I'm looking to take a file name with spaces and replace the spaces with escape characters (i.e. "\ ") to use for file checking and manipulation. I'm using Bourne Shell (not bash) and I cannot use the sed command for implementation and portability reasons. I tried using parameter expansion, but I couldn't get it to work and I know that it is much less robust than in bash.
Example:
ITEM="file with spaces" # What I have
WRAPPED_ITEM="file\ with\ spaces" # What I need
PATTERN="\ " # What I tried
WRAPPED_ITEM="${ITEM/ /$PATTERN}"
Are there any workarounds for this?
sedis unavailable, what can you use? The Bourne shell does not have sensible built-in features for this, though I suppose you could painstakingly do something with simple parameter expansions. Are you looking for a solution specifically for escaping spaces, or generally backslash a given set of characters, or even more generally substitute strings with other strings?printfhas the%qformat specifier which does roughly what you seem to be asking.cutin particular to be missing; it can't be many bytes of code.