I have a little script that reads a json file:
{
"path": "$HOME/Projects:$HOME/Github"
}
I want to read path
value, split on colon :
and then read out the two paths with $HOME
expanded.
#!/bin/sh
path_list="$(jq -r '.path' < "$JSON_FILE" | tr ':' '\n')"
echo "$path_list" | while IFS= read -r line; do
echo "$line"
done;
The output is not what I would expect:
$HOME/Projects
$HOME/Github
Yet when I run echo "$HOME/Projects"
the $HOME
parameter expands fine.
Initially, I thought that I needed double quotes around the variable so I tried echo "\"${line}\""
and that just prints "$HOME/projects"
. I am confused. Can anyone please shed some light on this for me or point to a good tutorial on bash parameter expansion?
Regarding another SO question addressing similar issue. I do not think this is the same because that OP was asking about expanding strings loaded from a file. I do not think that is the dominant concern in my question. Other responses to this question involve using eval
which I would like to because users will be entering their own inputs. Other solutions rely on external packages like gettext
. I believe there should be a straight forward answer here.