escaping unix filename for bash invocation

1,003 views
Skip to first unread message

gocss

unread,
Dec 8, 2015, 7:27:55 PM12/8/15
to golang-nuts
Is there a function that I can pass a unix filename string too
which will escape characters that may cause issues when invoking bash ??

example filename: 12%3!4"3 '< [> 3 |.txt

gocss

l

Marvin Stenger

unread,
Dec 8, 2015, 7:39:25 PM12/8/15
to golang-nuts
You are fine to backslash escape every character, so I guess you can implement a function for yourself easily.

Shawn Milochik

unread,
Dec 8, 2015, 7:45:50 PM12/8/15
to golang-nuts
On Tue, Dec 8, 2015 at 7:39 PM, Marvin Stenger <marvin.s...@gmail.com> wrote:
You are fine to backslash escape every character, so I guess you can implement a function for yourself easily.

I don't know if that's the best solution for this, but if it solves the problem, you can just use strings.Split and strings.Join to do it:



Jesse McNelis

unread,
Dec 8, 2015, 8:00:39 PM12/8/15
to gocss, golang-nuts
It's best to avoid invoking a shell if you can, much better to just
invoke the programs you want to run directly from Go.
If you're invoking programs directly from Go then you won't need to
escape the filename. You can just pass it in as an arg.

Paul Borman

unread,
Dec 8, 2015, 8:51:26 PM12/8/15
to Jesse McNelis, gocss, golang-nuts
It really depends on what you are trying to do with the file name, as indicated by other posters.  If you really are creating a command line that is to be directly parsed by bash then you can put the name in single quotes for the most part.  The exception is if you want a single quote, but that is not hard.  If you have the name %bob's file then it would become  '%bob'"'"'s file'.  See http://play.golang.org/p/ntk8EEGjfk for an example of how to do that.  But are you really sure you need to do this?


--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Tong Sun

unread,
Aug 21, 2017, 12:31:14 AM8/21/17
to golang-nuts
Thanks Paul, that's exactly what I really need to do. 
Reply all
Reply to author
Forward
0 new messages