0

I have a small problem: I enter the command tag -f F1 into my terminal which means using my tag programme find the tag f1 in the current directory. I've stored a list of all the tags and their paths in a separate file called tagslist which I read in order to print the path to the user like in the picture. However, the problem is I need to turn the F1 argument into $F1 so I can search for it in my file.

source $(dirname "$0")/tagslist
echo "the location of $tag1 is: $(dirname "$0")/$F1"

I can't just do:

tag=$2
source $(dirname "$0")/tagslist
echo "the location of $tag1 is: $(dirname "$0")/$tag"

as $tag does not exist in the tagslist file, only F1

If anyone would be willing to share some wizardry to fix this I'd be very grateful.

Jimbo
  • 5
  • 2

1 Answers1

1

To address the $tag variable name indirectly, you use ${!tag}.

See this very active topic already discussing: Dynamic variable names in Bash.

Here is a working version of your code sample:

#!/usr/bin/env bash

tag=$2

# shellcheck disable=SC1090 # dynamic source
source "${0%/*}/tagslist"

printf 'The location of %s is: %s\n' "$tag" "${0%/*}/${!tag}"
Léa Gris
  • 17,497
  • 4
  • 32
  • 41