0

I have read this Q&A, but it seems to always think the parameter as file.

I want to transform the command diff <(ls old) <(ls new) from this link to one function which receive two arguments, although it is somewhat redundant.

I tried this which is just arg replace:

d2c(){
        diff <($1) <($2)
}

then run d2c "objdump -d foo1" "objdump -d foo2" show d2c:1: no such file or directory: objdump -d foo1

with fifo , above problem still be there.

Q: How can I pass arg like '"objdump -d foo1"', then let it run as command?

  • Are you using zsh? – muru Jun 06 '23 at 11:43
  • For the use case presented here, I think you might just want to use eval: d2c () { diff <(eval "$1") <(eval "$2"); }, but if you're on zsh, you can also try d2c () diff <(${(z)1}) <(${(z)2}) – muru Jun 06 '23 at 11:55
  • @muru, you'd also need a Q (to remove quotes) and @ within quotes to preserve empty elements in addition to that z tokenisation flag, but that would mean one could only run simple commands so would have no advantage over eval. – Stéphane Chazelas Jun 06 '23 at 12:02
  • Thanks for offering so many solutions. I use bash (sorry for not clarifying this when posting.) And eval "$1" works for me. – An5Drama Jun 06 '23 at 12:36
  • The error message you provided seems to suggest that word splitting is not done with the unquoted $1, but that's not the default in bash, where it is in zsh. – muru Jun 06 '23 at 13:19

1 Answers1

0

Looks like you want those $1 and $2 to be interpreted as shell code as opposed to being the name of a command to execute, so you'd need:

d2c() {
  diff <(eval " $1") <(eval " $2")
}

(note the leading space to avoid problems with commands whose name starts with -).

You'll also want to make sure you use single quotes and not double quotes when passing those codes to d2c to remove the risk of introducing command injection vulnerabilities.

d2c 'echo "$var1"' 'echo "$var2"; echo "$var3"'

Not

d2c "echo $var1" "echo $var2; echo $var3"

For instance.