0

Running xtrace or set -x with Bash will print the expanded command:

$ bash -xc 'touch "alfa bravo"'
+ touch 'alfa bravo'

However Dash does not print the command that is actually run:

$ dash -xc 'touch "alfa bravo"'
+ touch alfa bravo

The printed command appears to create 2 files, while the command actually creates 1. Can Dash be made to print the commands that are actually run?

  • 1
    You can't! dash print expanded string under -x effect. – cuonglm Mar 9 '16 at 3:30
0

This is a pretty awful workaround, but seems to do the trick:

BEGIN {
  w = "\47"
  while (++i < ARGC) {
    x = split(ARGV[i], y, w)
    for (each in y) {
      if (y[each] ~ /[^[:alnum:]%+,./:=@_-]/)
        printf w y[each] w
      else
        printf y[each]
      if (each < x)
        printf "\\" w
    }
    if (i == ARGC - 1)
      printf RS
    else
      printf FS
  }
}

Result:

$ awk -f charlie.awk touch "alfa bravo"
touch 'alfa bravo'

$ awk -f charlie.awk touch 'alfa"bravo'
touch 'alfa"bravo'

$ awk -f charlie.awk touch "alfa'bravo"
touch alfa\'bravo
  • Related: unix.stackexchange.com/a/234059/38906 – cuonglm Mar 9 '16 at 4:34
  • @cuonglm thanks, but his solution is weird if the last character is a single quote "alfa'" -> 'alfa'\''' – Steven Penny Mar 9 '16 at 4:44
  • You can reuse that string for shell. It's weird for human, but good for the shell. – cuonglm Mar 9 '16 at 4:49
  • @cuonglm I just noticed, Bash does the same thing: bash -xc "touch alfa\'" -> touch 'alfa'\''' – Steven Penny Mar 10 '16 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.