Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to include variable in string that begins with $ for escaping

I found for bash, using $'string' allows for escape characters. I also want to use a variable in the string like $'string ${var}'. However, the variable is not expanded and the output is string ${var}. Is there a way to use a variable in this type of string?

The reason I am using the string method with the dollar sign in front is to use the hexcode for a custom font to get a symbol. The desired goal is shown below.

sybmol='\uF107'
echo $'\uF101 ${symbol}'

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The interpretation of \u happens when the string is defined. You should use that quoting with the definition of symbol itself:

symbol=$'\uF107'

Then you can simply use two different kinds of quoting when specifying the argument of echo.

echo $'\uF101'"${symbol}"

The two quoted strings are implicitly concatenated into a single word by the shell.

Note that $'...' expands the escape sequence immediately, while echo itself can (via the -e option) expand similar sequences.

symbol='\uF107'
echo -e '\uF101'"$symobl"
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading