Echo's special character list? Try to write "(a+b)*c=d;" to a txt file

Hi all,
I tried to write a string similar to this:

“(a+b)*c = d”

into a txt file. It took me many hours through trials and errors to figure that out. Here is my solution:

	var String strCommand ='\\"\\(a + b\\)*c = d\\;\\"'
	val String echoResult = executeCommandLine("/bin/sh@@-c@@echo " + strCommand + " > /etc/openhab2/scripts/bash/t.txt", 5000)

It turned out as you can see above that

" ( ) ;

seem to be special characters and require double \ \ to work properly. I just want to post it here so that people don’t run into the same problem.

My questions to the smart people out there:

1. Is there a list of these special characters somewhere?
2. Is there a better way to write a string into a txt file?

I just wish there is a print binding or something like that to make things easier for non-software people like me.

You can easily google “shell escaping” and get lots of answers for how the linux shell handles strings, either delimited with single or double quotes. Here is an example:

You will also learn, that strings with single quotes are the safe quotes, because there are much less characters interpreted as special characters, whereas with double quotes there are a lot of magic going on.

Last point is, that “/bin/sh” is on most systems a link to “bin/bash” or another default shell, that comes with the system. So bash does have basically the same escaping rules than shell but adds some specialties for bash.

So, if your system is using bash, then you can also consult this article:

Roundup is, that this is not specifically an openHAB topic but of the underlying OS and its installed shell.

One last word on this. The definition of the string is based on the language specification on strings. I cannot tell by your example, if you are using Xtend or JavaScript. Nevertheless, these escaping rules apply to the language specification of the programming language you are using and you will find also well defined descriptions of the syntax.

Hope this helps for a starter to get you in the position of getting an own insight on escaping without try and error.

@franks . Thank you for your explanations. They are useful. I have much to learn.

This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.