[CentOS] Special characters in bash strings

Mon Jul 11 21:53:47 UTC 2022
Young, Gregory <gregory.young at n-able.com>

Instead of using the ‘~’ in the file path, try using the ‘${HOME}’ variable instead. If you must use the ‘~’, you will likely need to escape it in many places. Another thing to note, if you use single quotes ( ‘ ) instead of double quotes ( “ ) , most places in bash scripts won’t try to expand the variables inside them. This is also helpful if you have a script calling SSH and want to pass a command to the remote server which contains a remote variable reference:

[bob at localhost bob]# ssh user at host ‘echo ${HOME}; exit’
[bob at localhost bob]# /home/user
[bob at localhost bob]# ssh user at host “echo ${HOME}; exit”
[bob at localhost bob]# /home/bob


From: CentOS <centos-bounces at centos.org> on behalf of H <agents at meddatainc.com>
Date: Wednesday, July 6, 2022 at 21:41
To: Centos Mailing List <centos at centos.org>
Subject: [CentOS] Special characters in bash strings
[EXTERNAL] This email originated from outside of the organization. Do not click links or open attachments unless you recognize the sender and know the content is safe.

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:


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