0

I'm trying to read a files content into a variable in my .sh.
Some part of the files path is stored in another variable.

This is my current code:

almvers=${cat $CI_PROJECT_DIR/pom-almversion.txt}

But I get this error:

${cat $CI_PROJECT_DIR/pom-almversion.txt}: bad substitution

What am I doing wrong?

nvplus
  • 81
  • 8
  • [Shellcheck](https://www.shellcheck.net/) identifies the problem, and shows exactly how to fix it. It's a good idea to run [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code. – pjh Jun 06 '23 at 20:13
  • `${ ... }` is for parameter expansion. – user1934428 Jun 07 '23 at 08:27

1 Answers1

1

There is no need for cat.


Consider doing the following:

almvers="$(<$CI_PROJECT_DIR/pom-almversion.txt)"
0stone0
  • 34,288
  • 4
  • 39
  • 64
  • Thanks, works now. I expected cat and < to do the same – nvplus Jun 06 '23 at 12:13
  • 2
    Problem was the `${...}` where you need `$(...)`. Avoiding cat is an optimization, see https://stackoverflow.com/questions/4749905/how-can-i-read-a-file-and-redirect-it-to-a-variable. – Walter A Jun 06 '23 at 12:16
  • [Shellcheck](https://www.shellcheck.net/) finds a potential problem in the code (missing quotes). The report includes a link to more information about the problem and how to fix it. It's a good idea to run [Shellcheck](https://www.shellcheck.net/) on all new and modified shell code. – pjh Jun 06 '23 at 20:10