-1

Is this the way to separate variables from other text in bash:

${PROJECT}_${GITCOMMIT}.tar.gz

if PROJECT = test AND GITCOMMIT = 222

Will this give you test_222.tar.gz? When should you use braces {} when it comes to variables? Are there other ways to separate the variables in bash from text?

Chris Hansen
  • 7,813
  • 15
  • 81
  • 165

2 Answers2

0

Yes, that is the way to do it. Your question is garbled however; you've got an underscore between the two variable expressions, so you won't get "test" and "222" adjacent to eachother in the resulting string.

Pointy
  • 405,095
  • 59
  • 585
  • 614
0

In general, the braces are how you delimit text in the variable name from following text. In you case,

$PROJECT$GITCOMMIT.tar.gz  # test222.tar.gz

would be sufficient, because the $ that starts GITCOMMIT can't be part of the preceding parameter name. (Same for the . just before tar; there can't be a variable named GITCOMMIT..) If $PROJECT were followed by literal text, the braces would be necessary:

$PROJECT_$GITCOMMIT.tar.gz  # WRONG: No variable PROJECT_
${PROJECT}_$GITCOMMIT.tar.gz # CORRECT: test_222.tar.gz
chepner
  • 497,756
  • 71
  • 530
  • 681