[CentOS] Special characters in bash strings

Thu Jul 7 04:52:13 UTC 2022
Jon LaBadie <jcu at labadie.us>

On Wed, Jul 06, 2022 at 09:41:14PM -0400, H wrote:
>I have run into a bash variable string problem that I think I have nailed down to the variable string containing a tilde (~). Not sure if my conclusion is correct and could use some help.
>To make a long(er) story short, an associative array variable was created:
>and referenced in the following format in the shell script:

Is there a reason you desire "delayed" evaluation of the tilde?

If no, then evaluate the tilde in the above assignment by not
quoting it.

If yes, then where tilde evaluation IS wanted, you will likely
need to do a second round of shell evaluation of the command
line by using the keyword "eval".

$ x="~/foo"
$ y=~"/foo"         # y contains the tilde evaluated version

$ echo "$x $y"
~/foo /home/jon/foo

$ echo $x $y        # quotes don't matter here
~/foo /home/jon/foo

$ eval echo "$x $y"
/home/jon/foo /home/jon/foo

Use of eval could introduce other unexpected/unwanted
evaluations and is discouraged unless required.

Jon H. LaBadie                  jcu at labadie.us