after all, what could be more exciting?

okay, so where we last left off, we had something like:

    # escape any single quotes in an argument
    quote(){
      echo "$1" | sed -e "s,','\\\\'',g"
    }

    # save up a properly quoted/escaped version of "$@"
    for arg in "$@"; do
      saved="${saved:+$saved }'$(quote "$arg")'"
    done

i got some really interesting feedback from a few people; Loïc Minier who has some similar code that he cuts and pastes on an as-needed basis, and Ralf Wildenhues who has overseen some similar code that exists in the autoconf system.

a notable feature in Loïc's solution was to quote the string entirely within the escaping function, whereas in mine the result was a "quotable" string (i.e. the quotes were put around the output afterwards).

> escape() {
>     echo "$*" | sed "s/'/'\"'\"'/g; s/.*/'&'/"
> }

Ralf pointed out one potential problem with both of these approaches. on the topic of portability, there are some implementations of echo that will directly interpret control characters ('\n', '\t', etc) and others not. he suggested that "printf" be used instead:

- echo "$1" | sed -e "s,','\\\\'',g"
+ printf '%s\n' "$1" | sed -e "s,','\\\\'',g"

though there's no reason i can think of not to take that a bit further and use a herestring, which could save an extra fork if printf isn't a builtin:

    quote(){
      sed -e "s,','\\\\'',g" << EOF
    $1
    EOF
    }

also, Ralf suggested in the interest of efficiency that some kind of case statement be used to avoid unnecessary forks when an argument doesn't contain any "'" characters. this is apparently a trick used in autoconf-generated scripts:

    case "$arg" in
    *\'*)
      saved="${saved:+$saved }'$(quote "$arg")'"
      ;;
    *)
      saved="${saved:+$saved }'$arg'"
      ;;
    esac

finally, a lingering problem which hasn't been solved on the autoconf side of things (nor in my previous solution) was that in the case that an argument contained trailing newlines, such whitespace would be silently discarded in the quoted version (this is due to how shell command substitution works in general). but really, this is quite the corner case... how often does one run a command line with both an escaped quote and trailing newlines in an argument? an example:

rangda[/home/sean] ./good.sh "isn't very common" "to give args" "with trailing \\n's:

"
original command line, 3 arguments: isn't very common to give args with trailing \n's:


arg: isn't very common
arg: to give args
arg: with trailing \n's:


mangled command line, 0 arguments:
restored command line, 3 arguments: isn't very common to give args with trailing \n's:
arg: isn't very common
arg: to give args
arg: with trailing \n's:
rangda[/home/sean]                                                           :)

however, i noticed that a slightly modified version of Loïc's approach might not suffer from this problem because the string would be quoted before being returned (thus any newlines would be enclosed in quotes that would preserve them). I mentioned this to Ralf who will be bringing it up on the autoconf-patches mailing list... sounds promising anyway. so i give you the combined improved version:

#!/bin/sh -e

# escape any single quotes in an argument and then quote it
quote(){
  sed -e "s,','\\\\'',g; 1s,^,',; \$s,\$,',;" << EOF
$1
EOF
}

# save up a properly quoted/escaped version of "$@"
for arg in "$@"; do
  case "$arg" in
  # when a string contains a "'" we have to escape it
  *\'*)
    saved="${saved:+$saved }$(quote "$arg")"
    ;;
  # otherwise just quote the variable
  *)
    saved="${saved:+$saved }'$arg'"
    ;;
  esac
done

echo original command line, $# arguments: "$@"

while test -n "$*"; do echo arg: "$1"; shift; done
echo mangled command line, $# arguments: "$@"

# restore the cmdline
eval set -- "$saved"
echo restored command line, $# arguments: "$@"
while test -n "$*"; do echo arg: "$1"; shift; done

output from the same example cmdline:

rangda[/home/sean] ./better.sh "isn't very common" "to give args" "with trailing \\n's:

"
original command line, 3 arguments: isn't very common to give args with trailing \n's:


arg: isn't very common
arg: to give args
arg: with trailing \n's:


mangled command line, 0 arguments:
restored command line, 3 arguments: isn't very common to give args with trailing \n's:


arg: isn't very common
arg: to give args
arg: with trailing \n's:


rangda[/home/sean]                                                           :)

Very nice site!
Comment by Pharme506 Fri Jan 1 07:00:25 2010

Hello! , , , , ,

Comment by Anonymous Fri Jan 8 11:32:47 2010

Hello! , , , , ,

Comment by Anonymous Mon Jan 11 09:13:19 2010

Hello! xanax , tramadol , xanax , tramadol , cialis ,

Comment by xanax Thu Jan 14 16:28:56 2010
Comment by viagra Thu Jan 14 16:42:08 2010
Comment by generic_viagra Thu Jan 14 16:52:33 2010
Comment by cialis Thu Jan 14 17:15:08 2010
Comment by cialis Thu Jan 14 17:21:22 2010
Comment by buy_cialis Thu Jan 14 17:34:33 2010

Hello! tramadol , viagra , cialis , xanax , tramadol ,

Comment by tramadol Thu Jan 14 17:42:02 2010
Comment by cialis Thu Jan 14 17:49:04 2010
Comment by cheap_cailis Thu Jan 14 17:56:17 2010
Comment by viagra Mon Jan 18 15:06:45 2010
Very nice site!
Comment by Pharmf355 Wed Jan 20 13:30:39 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 02:36:21 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 02:44:01 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 02:50:22 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 02:56:35 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:03:03 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:09:30 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:15:50 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:22:00 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:28:04 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:34:18 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:41:05 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:48:14 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 03:54:25 2010

Hello! , , , , ,

Comment by John716 Wed Jan 27 04:00:28 2010

Hello! ,

Comment by Anonymous Wed Jan 27 04:17:51 2010

Hello! ,

Comment by Anonymous Wed Jan 27 04:18:51 2010

Hello! ,

Comment by Anonymous Wed Jan 27 04:19:12 2010

Hello! ,

Comment by Anonymous Wed Jan 27 04:19:34 2010

Hello! ,

Comment by Anonymous Wed Jan 27 05:02:33 2010

Hello! ,

Comment by Anonymous Wed Jan 27 05:02:58 2010

Hello! ,

Comment by Anonymous Wed Jan 27 05:03:12 2010

Hello! ,

Comment by Anonymous Wed Jan 27 05:03:26 2010

Hello! ,

Comment by Anonymous Wed Jan 27 05:03:40 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 05:36:17 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 05:42:15 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 05:49:34 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 05:56:48 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:04:05 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:11:11 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:18:17 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:25:56 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:32:36 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:38:44 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:44:24 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:50:10 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 06:55:29 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:01:12 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:07:03 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:12:31 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:18:12 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:23:37 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:29:08 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:34:37 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:40:16 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:45:52 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:51:49 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 07:57:35 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 08:03:33 2010

Hello! , , , , ,

Comment by Anonymous Wed Jan 27 08:08:08 2010
Comment by 669.666666666667 Thu Feb 11 12:40:30 2010
gGbcqEMG
Comment by wqWeHdG Thu Feb 18 16:49:26 2010
Comment by 669.666666666667 Sat Feb 27 17:19:46 2010
gaCzLD sxzQik
Comment by viagra Sat Feb 27 20:34:44 2010
WhJTxZAJ HQnhXJH
Comment by viagra Sat Feb 27 20:36:01 2010
Comment by Diesel Mon Mar 1 23:25:39 2010
Comment by Diesel Mon Mar 1 23:26:50 2010
Comment by Diesel Mon Mar 1 23:27:18 2010
Comment by Diesel Mon Mar 1 23:27:37 2010
Comment by Diesel Mon Mar 1 23:27:59 2010
Comment by Jane Tue Mar 2 20:53:56 2010
Comment by Jane Tue Mar 2 20:54:52 2010
Comment by Jane Tue Mar 2 20:55:12 2010
Comment by Jane Tue Mar 2 20:55:33 2010
Comment by Jane Tue Mar 2 20:55:52 2010
Comment by Dominic Fri Mar 5 12:19:39 2010
Comment by Dominic Fri Mar 5 12:20:09 2010
Comment by Dominic Fri Mar 5 12:20:28 2010
Comment by Dominic Fri Mar 5 12:20:47 2010
Comment by Dominic Fri Mar 5 12:21:08 2010
Comment by Dominic Fri Mar 5 12:50:16 2010
Comment by Dominic Fri Mar 5 12:51:19 2010
Comment by Dominic Fri Mar 5 12:52:02 2010
Comment by Dominic Fri Mar 5 12:52:33 2010
Comment by Dominic Fri Mar 5 12:53:05 2010
Comment by Kir Fri Mar 5 13:14:01 2010
Comment by Kir Fri Mar 5 13:14:44 2010
Comment by Kir Fri Mar 5 13:15:05 2010
Comment by Kir Fri Mar 5 13:15:28 2010
Comment by Kir Fri Mar 5 13:15:57 2010
Comment by Dominic Fri Mar 5 13:22:17 2010
Comment by Dominic Fri Mar 5 13:22:59 2010
Comment by Dominic Fri Mar 5 13:23:25 2010
Comment by Dominic Fri Mar 5 13:23:53 2010
Comment by Dominic Fri Mar 5 13:24:23 2010
Comment by Arnie Fri Mar 5 14:16:41 2010
Comment by Arnie Fri Mar 5 14:17:42 2010
Comment by Arnie Fri Mar 5 14:18:11 2010
Comment by Arnie Fri Mar 5 14:18:41 2010
Comment by Arnie Fri Mar 5 14:19:19 2010
Comment by Arnie Fri Mar 5 14:22:25 2010
Comment by Arnie Fri Mar 5 14:23:36 2010
Comment by Arnie Fri Mar 5 14:24:13 2010
Comment by Arnie Fri Mar 5 14:24:47 2010
Comment by Arnie Fri Mar 5 14:25:13 2010
Comment by Aron Fri Mar 5 14:28:55 2010
Comment by Aron Fri Mar 5 14:29:55 2010
Comment by Aron Fri Mar 5 14:30:31 2010
Comment by Aron Fri Mar 5 14:31:05 2010
Comment by Aron Fri Mar 5 14:32:06 2010
Comment by Arnie Fri Mar 5 14:38:02 2010
Comment by Arnie Fri Mar 5 14:38:50 2010
Comment by Arnie Fri Mar 5 14:39:21 2010
Comment by Kir Fri Mar 5 14:39:53 2010
Comment by Kir Fri Mar 5 14:40:20 2010
Comment by Arnie Fri Mar 5 14:40:48 2010
Comment by Arnie Fri Mar 5 14:41:18 2010
Comment by Kir Fri Mar 5 14:41:55 2010
Comment by Kir Fri Mar 5 14:42:57 2010
Comment by Kir Fri Mar 5 14:43:36 2010
Comment by Aron Fri Mar 5 15:02:10 2010
Comment by Aron Fri Mar 5 15:02:53 2010
Comment by Aron Fri Mar 5 15:03:22 2010
Comment by Aron Fri Mar 5 15:03:55 2010
Comment by Jane Fri Mar 5 15:06:25 2010
Comment by Jane Fri Mar 5 15:06:54 2010
Comment by Jane Fri Mar 5 15:07:20 2010
Comment by Jane Fri Mar 5 15:07:52 2010
Comment by Jane Fri Mar 5 15:08:20 2010
Comment by Bill Fri Mar 5 15:29:52 2010
Comment by Bill Fri Mar 5 15:30:56 2010
Comment by Bill Fri Mar 5 15:31:34 2010
Comment by Bill Fri Mar 5 15:32:08 2010
Comment by Bill Fri Mar 5 15:32:47 2010
Comment by Halo Fri Mar 5 17:17:12 2010
Comment by Halo Fri Mar 5 17:17:57 2010
Comment by Bill Fri Mar 5 17:18:30 2010
Comment by Bill Fri Mar 5 17:18:59 2010
Comment by Halo Fri Mar 5 17:19:30 2010
Comment by Bill Fri Mar 5 17:20:01 2010
Comment by Bill Fri Mar 5 17:20:34 2010
Comment by Bill Fri Mar 5 17:21:09 2010
Comment by Halo Fri Mar 5 17:21:51 2010
Comment by Aron Fri Mar 5 17:47:57 2010
Comment by Aron Fri Mar 5 17:48:52 2010
Comment by Aron Fri Mar 5 17:49:23 2010
Comment by Aron Fri Mar 5 17:49:58 2010
Comment by Aron Fri Mar 5 17:50:31 2010
Comment by Jane Fri Mar 5 17:51:08 2010
Comment by Jane Fri Mar 5 17:51:45 2010
Comment by Jane Fri Mar 5 17:52:21 2010
Comment by Jane Fri Mar 5 17:52:56 2010
Comment by Jane Fri Mar 5 17:53:34 2010
Comment by Hero Fri Mar 5 19:59:00 2010
Comment by Hero Fri Mar 5 20:00:20 2010
Comment by Hero Fri Mar 5 20:01:10 2010
Comment by Arnie Fri Mar 5 20:11:47 2010
Comment by Arnie Fri Mar 5 20:12:18 2010
Comment by Arnie Fri Mar 5 20:12:50 2010
Comment by Arnie Fri Mar 5 20:13:21 2010
Comment by Arnie Fri Mar 5 20:13:53 2010
Comment by Halo Fri Mar 5 20:33:21 2010
Comment by Halo Fri Mar 5 20:34:29 2010
Comment by Halo Fri Mar 5 20:35:07 2010
Comment by Halo Fri Mar 5 20:35:51 2010
Comment by Halo Fri Mar 5 20:36:32 2010
Comment by Halo Fri Mar 5 20:54:01 2010
Comment by Halo Fri Mar 5 20:54:52 2010
Comment by Halo Fri Mar 5 20:55:35 2010
Comment by Halo Fri Mar 5 20:56:15 2010
Comment by Halo Fri Mar 5 20:56:55 2010
Comment by Aron Fri Mar 5 21:33:08 2010
Comment by Aron Fri Mar 5 21:34:06 2010
Comment by Aron Fri Mar 5 21:34:55 2010
Comment by Aron Fri Mar 5 21:35:42 2010
Comment by Aron Fri Mar 5 21:36:30 2010
Comment by Bill Fri Mar 5 21:49:21 2010
Comment by Bill Fri Mar 5 21:50:29 2010
Comment by Bill Fri Mar 5 21:51:07 2010
Comment by Bill Fri Mar 5 21:51:45 2010
Comment by Bill Fri Mar 5 21:52:25 2010
Comment by Halo Fri Mar 5 22:15:26 2010
Comment by Halo Fri Mar 5 22:16:27 2010
Comment by Halo Fri Mar 5 22:17:12 2010
Comment by Halo Fri Mar 5 22:17:54 2010
Comment by Halo Fri Mar 5 22:18:33 2010
Comment by Hero Fri Mar 5 22:36:18 2010
Comment by Hero Fri Mar 5 22:37:29 2010
Comment by Hero Fri Mar 5 22:38:42 2010
Comment by Hero Fri Mar 5 22:39:48 2010
Comment by Hero Fri Mar 5 22:40:35 2010
Comment by Aron Fri Mar 5 22:47:01 2010
Comment by Aron Fri Mar 5 22:48:01 2010
Comment by Aron Fri Mar 5 22:48:54 2010
Comment by Aron Fri Mar 5 22:49:34 2010
Comment by Aron Fri Mar 5 22:50:15 2010
Comment by Arnie Fri Mar 5 22:59:10 2010
Comment by Arnie Fri Mar 5 23:00:36 2010
Comment by Arnie Fri Mar 5 23:01:26 2010
Comment by Heel Fri Mar 5 23:30:13 2010
Comment by Heel Fri Mar 5 23:31:20 2010
Comment by Heel Fri Mar 5 23:32:04 2010
Comment by Heel Fri Mar 5 23:32:48 2010
Comment by Heel Fri Mar 5 23:33:47 2010
Comment by Arnie Fri Mar 5 23:43:59 2010
Comment by Arnie Fri Mar 5 23:45:12 2010
Comment by Arnie Fri Mar 5 23:46:06 2010
Comment by Arnie Fri Mar 5 23:46:52 2010
Comment by Arnie Fri Mar 5 23:47:35 2010
Comment by Dominic Fri Mar 5 23:48:32 2010
Comment by Dominic Fri Mar 5 23:49:16 2010
Comment by Dominic Fri Mar 5 23:50:05 2010
Comment by Dominic Fri Mar 5 23:50:51 2010
Comment by Dominic Fri Mar 5 23:51:40 2010
Comment by Dominic Fri Mar 5 23:53:31 2010
Comment by Dominic Fri Mar 5 23:54:18 2010
Comment by Dominic Fri Mar 5 23:55:03 2010
Comment by Dominic Fri Mar 5 23:55:47 2010
Comment by Dominic Fri Mar 5 23:56:32 2010
Comment by Kir Sat Mar 6 01:05:55 2010
Comment by Kir Sat Mar 6 01:06:52 2010
Comment by Kir Sat Mar 6 01:07:35 2010
Comment by Kir Sat Mar 6 01:08:19 2010
Comment by Kir Sat Mar 6 01:09:02 2010
Comment by Kir Sat Mar 6 03:00:29 2010
Comment by Kir Sat Mar 6 03:01:41 2010
Comment by Dominic Sat Mar 6 03:13:53 2010
Comment by Dominic Sat Mar 6 03:14:40 2010
Comment by Dominic Sat Mar 6 03:15:34 2010
Comment by Dominic Sat Mar 6 03:16:22 2010
Comment by Dominic Sat Mar 6 03:17:10 2010
Comment by Aron Sat Mar 6 03:23:02 2010
Comment by Aron Sat Mar 6 03:23:49 2010
Comment by Aron Sat Mar 6 03:24:36 2010
Comment by Aron Sat Mar 6 03:25:24 2010
Comment by Aron Sat Mar 6 03:26:13 2010
Comment by Hero Sat Mar 6 03:34:08 2010
Comment by Hero Sat Mar 6 03:35:03 2010
Comment by Hero Sat Mar 6 03:36:05 2010
Comment by Hero Sat Mar 6 03:37:03 2010
Comment by Aron Sat Mar 6 04:22:27 2010
Comment by Aron Sat Mar 6 04:23:32 2010
Comment by Aron Sat Mar 6 04:24:23 2010
Comment by Aron Sat Mar 6 04:25:12 2010
Comment by Aron Sat Mar 6 04:26:05 2010
Comment by Kir Sat Mar 6 04:48:10 2010
Comment by Kir Sat Mar 6 04:49:32 2010
Comment by Kir Sat Mar 6 04:50:26 2010
Comment by Kir Sat Mar 6 04:51:18 2010
Comment by Kir Sat Mar 6 04:52:11 2010
Comment by Aron Sat Mar 6 05:02:28 2010