1

I have a unix shell variable, FOO = "bar", which I want to insert in a command directly in front of an underscore. I implemented this like so:

$ program.py --in_path ~/dir/$FOO_file_20210607.csv

This interprets the variable being dereferenced as $FOO_file_20210607, not $FOO.

Is there a way to parenthesize FOO to isolate it from the following statement?

1 Answers1

2

$FOO is shorthand for ${FOO} when dropping the braces doesn't introduce an ambiguity. Doing so does here, so you should keep them.

program.py --in_path ~/dir/${FOO}_file_20210607.csv

Or, since $FOO should be quoted anyway to protect against unexpected word-splitting or pathname expansion, you can simply use quotes, which will also be sufficient to isolate FOO as the parameter name as a side effect.

program.py --in_path ~/dir/"$FOO"_file_20210607.csv
chepner
  • 497,756
  • 71
  • 530
  • 681