Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

share|improve this question
1  
You can't! dash print expanded string under -x effect. – cuonglm Mar 9 at 3:30
up vote 0 down vote accepted

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
share|improve this answer
    
Related: unix.stackexchange.com/a/234059/38906 – cuonglm Mar 9 at 4:34
    
@cuonglm thanks, but his solution is weird if the last character is a single quote "alfa'" -> 'alfa'\''' – Steven Penny Mar 9 at 4:44
    
You can reuse that string for shell. It's weird for human, but good for the shell. – cuonglm Mar 9 at 4:49
    
@cuonglm I just noticed, Bash does the same thing: bash -xc "touch alfa\'" -> touch 'alfa'\''' – Steven Penny Mar 10 at 3:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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