0

Minimal example:

f="/tmp/a file"
cmd="cat \"$f\""

set -x

$cmd

How do I correctly handle $f here such that cmd becomes cat "/tmp/a file" (double or single quote).

cmd="cat '$f'" also doesn't work.

Daanturo
  • 124
  • 2
  • 7
  • This might help: [Why should eval be avoided in Bash, and what should I use instead?](https://stackoverflow.com/q/17529220/3776858) – Cyrus Feb 26 '23 at 07:09
  • 3
    Don't store commands in variables, just execute them directly (or use a function or possibly an array instead). Variables are for storing data, functions are for storing executable code. See [BashFAQ #50: "I'm trying to put a command in a variable, but the complex cases always fail!"](http://mywiki.wooledge.org/BashFAQ/050) and ["Why does shell ignore quoting characters in arguments passed to it through variables?"](https://stackoverflow.com/questions/12136948/why-does-shell-ignore-quoting-characters-in-arguments-passed-to-it-through-varia) – Gordon Davisson Feb 26 '23 at 08:30

0 Answers0