If you're able to pass your unquoted command to a function using awk
, sed
, or something similar it could be possible to fix the quoting or escape it if the path is always within the same two options:
#!/bin/env bash
# quote_path.sh
# wraps quotes or escapes an unquoted path in a command
# takes command with unquoted path and wraps in quotes
quoted_cmd()
{
quoted=$(echo $cmd | awk '{ sub(/ -f/,"\" -f"); sub(/-i /,"-i \""); print }')
# eval $quoted
escape_cmd # comment out if using eval $quoted
}
# takes quoted_cmd() and escapes it (as an alternative to wrapping in quotes)
escape_cmd()
{
escape=$(echo $quoted | awk 'BEGIN { FS="-i \"|\" -f" } ; { print $2 }')
concat=$(echo $escape | sed 's! !\\\\ !g')
cmdesc=$(echo $quoted | awk '{ sub(/".*" -f/," -f"); $2="-i '"${concat}"'"; print }')
eval $cmdesc
}
cmd="$@"
quoted_cmd
So with this you could pass in your command as argument (as a string or variable):
$ ./quote_path.sh ffmpeg -i /path to file /path to file -f wav -ar 16000 "foobar" ...
As Etan Reisner and that other guy mentioned, it should likely be reported as a bug, in the meantime it is what it is... give it a try!
Update: I was able to revise the function a bit more so that it is less likely to fail in the event another part of the command has an argument with quotes, contains additional slashes, etc. The command shouldn't require being quoted when passing it as an argument (optional) and will produce a quoted or an escaped path in the command. After invoking the function the entire path was returned, concatenated with the rest of the command.
Result:
ffmpeg -i "/path to file /path to file" -f wav -ar 16000 ... # quoted_cmd()
ffmpeg -i /path\ to\ file\ /path\ to\ file -f wav -ar 16000 ... # escape_cmd()
...
ffmpeg version 2.5.3-tessus Copyright (c) 2000-2015 the FFmpeg developers
...