terminal - BASH function for escaping spaces in filenames before opening them -
i've been trying write function bash profile quite time now. problem i'm trying overcome i'm provided file paths include spaces , it's pain having go through , escape spaces before try open in terminal.
e.g. file -> /volumes/company/illustrators/website front page design.ai
what i'm trying end '/volumes/company/illustrators/website\ front\ page\ design.ai' being opened terminal.
so far i've managed escape spaces out, error "the file ..... not exist."
my code far
function opn { open "${1// /\\ }";}
any appreciated.
the important thing understand difference between syntax , literal data.
when done correctly, escaping syntax: it's read , discarded shell. is, when run
open "file spaces"
or
open file\ with\ spaces
or even
open file" "with\ spaces
...the quoting , escaping parsed , removed shell, , actual operating system call gets executed this:
execv("/usr/bin/open", "open", "file spaces")
note there aren't backslashes (or literal quotes) in syscall's arguments! if put literal backslashes in data, cause run:
/* c syntax, "\\" single-character backslash literal */ execv("/usr/bin/open", "open", "file\\ with\\ spaces")
...and unless there's file backslashes in name, doesn't work, giving "file not exist" error report.
so -- call open name in quotes:
open "$1"
...there's no need opn
wrappper.
Comments
Post a Comment