Solved Glitch when using sysrc to set system variables

I have stumbled on a glitch of sysrc (apparently).
I am trying to set a system variable to a string containing quotes like so:
Bash:
 # sysrc -f "$rootDir/etc/rc.conf" ssh_tunnel_connection_1="abc \"def\" gah"
ssh_tunnel_connection_1: abc def gah -> abc def gahdef gah

 # sysrc -f "$rootDir/etc/rc.conf" ssh_tunnel_connection_1="abc \"def\" gah"
ssh_tunnel_connection_1: abc def gahdef gah -> abc def gahdef gahdef gah

 # sysrc -f "$rootDir/etc/rc.conf" ssh_tunnel_connection_1="abc \"def\" gah"
ssh_tunnel_connection_1: abc def gahdef gahdef gah -> abc def gahdef gahdef gahdef gah

 # sysrc -f "$rootDir/etc/rc.conf" ssh_tunnel_connection_1="abc \"def\" gah"
ssh_tunnel_connection_1: abc def gahdef gahdef gahdef gah -> abc def gahdef gahdef gahdef gahdef gah
As you can see, after each call the variable value gets garbled with its previous content.
Am I doing anything wrong? Are quotes admissible within rc.conf values?
 
Are quotes admissible within rc.conf values?
rc.conf is a shell script. It typically only contains variable assignments but it's still a shell script. So it follows the rules of sh(1) with regards to variable assignments.

I'm thinking sysrc(8) itself is perhaps failing to parse it correctly. Have you tried to use single quotes inside the double quotes? Those don't need to be escaped. Speaking of being escaped, the shell might be escaping the double quotes before the arguments are passed to sysrc(8). You may need to escape the escape. Which is going to turn into a backslash hell. I'd use single quotes to circumvent that.

Some things to try:
sysrc -f "$rootDir/etc/rc.conf" ssh_tunnel_connection_1="abc \\\"def\\\" gah"
sysrc -f "$rootDir/etc/rc.conf" ssh_tunnel_connection_1="abc 'def' gah"
 
Thank you. Using single quotes solves the problem, I think I will use that.
Double-escaping works only the first time, then the problem appears again (apparently the doubly escaped quotes are unescaped after the second call).
I understand how this can be very tricky to implement correctly in the sysrc script. IMHO the best solution would be to simply document that quotes are inadmissible within variable values.
 
Back
Top