Use variable in filename with underscore


In my script I name an argument $out and then trying to use it in a filename like this:

My problem is that $out_ is recognized as “not only $out“. The file name MUST be like this.
I already tried quotations or double quotations or + with no appropriate results.


Multiple options.

  1. Use {} to qualify the variable name (as pointed out by @PetSerAl):
  2. Use the -f operator to expand $out before placing it in the string:
    'C:\folder\{0}_date.txt' -f $out
  3. Use the backtick () escape character to stop parsing of the variable name:
  4. Use a sub-expression ($()) to evaluate the variable:


Use variable in filename with underscore by licensed under CC BY-SA | With most appropriate answer!

Leave a Reply