[CentOS] Special characters in bash strings

Fri Jul 8 02:10:21 UTC 2022
H <agents at meddatainc.com>

On 07/06/2022 11:27 PM, Chris Schanzle wrote:
> Agree with Gordon's other response having specific examples of where it worked and didn't work would help us help you, but these things can get messy quickly.
>
> Since you quoted the tilde path, it is not expanded when assigned to your array.  Take out the quotes to have it expanded at the time of assignment.  If instead you want it expanded later, use eval.
>
> x="~/.bashrc"
> $ echo "$x" $x; ls -l $x
> ~/.bashrc ~/.bashrc
> ls: cannot access '~/.bashrc': No such file or directory
>
> $ eval ls -l $x
> -rw-r--r-- 1 schanzle mygroup 8720 Mar  4 15:46 /home/schanzle/.bashrc
>
> $ (ls -l $x)
> ls: cannot access '~/.bashrc': No such file or directory
>
> $ bash -c "ls -l $x"
> -rw-r--r-- 1 schanzle mygroup 8720 Mar  4 15:46 /home/schanzle/.bashrc
>
>
> # no quotes = expansion at assignment
> $ x=~/.bashrc
> $ echo "$x" $x; ls -l $x
> /home/schanzle/.bashrc /home/schanzle/.bashrc
> -rw-r--r-- 1 schanzle mygroup 8720 Mar  4 15:46 /home/schanzle/.bashrc
>
>
> Hope that helps,
> Chris
>
> On 7/6/22 9:41 PM, 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:
>>
>> p[work_path]="~/projects/test/"
>>
>> and referenced in the following format in the shell script:
>>
>> "${p[work_path]}"
>>
>> To my consternation this worked fine in some places but not in others. I tried to use the above construct when piping output, as part of a file reference when calling psql from the command line and when referencing an xslt file with xsltproc.
>>
>> In some places it worked, in others it did not but when I substituted the variable reference above with the path in clear text itself it then worked.
>>
>> It looks like there are some nuances on variable substitution that I have yet to learn, perhaps tied to the use of the tilde since using the variable p[work_path]="/home/user/projects/test/" seemed to work in all places.
>>
>> Pointers welcome!
>>
>> _______________________________________________
>> CentOS mailing list
>> CentOS at centos.org
>> https://lists.centos.org/mailman/listinfo/centos

Thank you, I read up on bash expansion of tilde and realized substituting $HOME for ~ would be the best and would avoid any other unforeseen complications.

Once I had done that the script worked.