It Seemed Like a Good Idea at the Time Coding, Mostly

25Jun/090

Escaping Strings in a Shell Pipeline

A useful little sed script I wrote, which turned out to be much more useful than I expected. It simply takes a list of files, one per line, and outputs that same list, but with all the spaces and special characters escaped. It's mainly intended for use right between find and xargs, as in "find . -name 'asd' -print | shell-escape | xargs -I % echo %'

The list of characters is taken from IEEE Std 1003.1

#!/bin/sed -f
#
# Shell-Escape
# A very basic script to escape characters in the shell.
# Mainly intended for use in shell pipelines, right between
# 'find' and 'xargs'
#

s@\\@\\\\@g
s@ @\\ @g
s@'@\'@g
s@"@\\"@g
s@(@\\(@g
s@)@\\)@g
s@|@\\|@g
s@&@\\&@g
s@;@\\;@g
s@<@\\<@g
s@>@\\>@g
s@`@\\`@g
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)
Tagged as: , , No Comments