String 如何在tcl中输出带有{}的完整字符串

String 如何在tcl中输出带有{}的完整字符串,string,tcl,String,Tcl,我有一个特殊字符行,我想做的puts和换行在1行代码的一切。 我如何做到这一点,而不必关心特殊字符或将其拆分为多行 例如: puts $infile {I_want_$_"put_exactly_this_out$"_to_$infile\nI_also$#_"\n} 以下是输出文件的外观: I want to eat_1_2_$ "I want to sleep" I go to school\ 所有以上,我想做一个放在1线。因此,在

我有一个特殊字符行,我想做的puts和换行在1行代码的一切。 我如何做到这一点,而不必关心特殊字符或将其拆分为多行

例如:

puts $infile {I_want_$_"put_exactly_this_out$"_to_$infile\nI_also$#_"\n}

以下是输出文件的外观:

I want to eat_1_2_$

"I want to sleep"

I go to school\
所有以上,我想做一个放在1线。因此,在脚本中,我尝试执行以下操作,但似乎不起作用

puts $outfile1 "I want to eat_1_2_$\n"I want to sleep"\nI go to school\"

puts $outfile1 {I want to eat_1_2_$\n"I want to sleep"\nI go to school\}

要输出的引号需要转义: 因为您有需要使用双引号解释的字符(\n) 在这种情况下更容易

puts stdout "I want to eat_1_2_\$\n\"I want to sleep\"\nI go to school\\"
当您有混合特殊字符、转义序列和变量的复杂字符串时,通常更容易逐段构建它们

set x {I want to eat_1_2_$}
append x \n
append x {"I want to sleep"}
append x \n
append x {I go to school\\}
puts stdout $x
你也可以这样做:

puts $out [string cat {I want to eat_1_2_$} \n {"I want to sleep"} \n {I go to school} \\]


您希望打印的字符串是什么样子的?在极端情况下,您可以将所有内容都放在双引号中,并在所有要按原样输出的Tcl元字符前面加一个反斜杠。我只想节省时间,只需执行一行PUTS
puts $out [join {{I want to eat_1_2_$} {"I want to sleep"} "I go to school\\"} \n]