Glebbb
Glebbb

Reputation: 356

How can I accept unquoted strings containing backslashes?

I want a command to convert from windows to unix filenames, simply to replace backslashes with frontslashes... but without quoting the argument with "" because that's a chore when copy-pasting.

It works in the other direction (u2w) with the input quoted and without, but not for w2u.

machine:~/glebbb> w2u "a\b\c"

a/b/c

machine:~/glebbb> w2u a\b\c  

abc

How can I make it work? I tried every form of escaping, echo -E, printf etc, nothing seems to work!

function w2u {
    if [ -z "$1" ] ; then
        echo "w2u: must provide path to convert!"
        return 1
    else
        printf "\n%s\n\n" "$1" | sed -e 's#\\#\/#g'
        return 0
    fi
}

Upvotes: 1

Views: 75

Answers (1)

David
David

Reputation: 1065

If you're copy-pasting and the path is contained in the X clipboard, you can use xclip:

xclip -o | sed -e 's#\\#\/#g'

If you've got a ton of file paths to convert, you can process the whole file instead:

sed ... < file

will produce a new stream with the backslashes changed to slashes.

Otherwise I can't think of any way how to not-escape the parameters to w2u and yet have backslashes lose their meaning.

Upvotes: 1

Related Questions