在Tcl中使用反斜杠换行符序列

时间:2014-09-23 07:32:17

标签: tcl newline substitution backslash

在Tcl中,我们使用反斜杠来转义特殊字符以及在多行中传播长命令。

例如,典型的if循环可以写为

set some_Variable_here  1
if { $some_Variable_here == 1 } { 
    puts "it is equal to 1"
} else { 
    puts "it is not equal to 1"
}

在反斜杠的帮助下,它也可以写成如下

set some_Variable_here  1
if { $some_Variable_here == 1 } \
{ 
    puts "it is equal to 1"
} \
else { 
    puts "it is not equal to 1"
}

因此,使用反斜杠我们可以将语句视为好像它们在同一行中一样。

让我们考虑set声明

我可以写下面的内容

set x Albert\ Einstein;# This works
puts $x

#This one is not working
set y Albert\
Einstein

如果我尝试使用双引号或大括号,那么上面的方法就可以了。那么,是否有可能在没有双引号或括号的情况下使用反斜杠转义换行符?

1 个答案:

答案 0 :(得分:2)

反斜杠 - 换行符 - 空白*序列(即跳过空格后)始终用单个空格替换。要在结果字符串中使用反斜杠后跟换行符,请使用\\后跟\n

set y Albert\\\nEinstein