Sending Strings That Look Like Flags
Because the send command looks for special arguments such as -s and -h, you cannot send strings like these without taking some special action. An argument of "--" (two hyphens) forces send to interpret the following argument literally.[44] So if you want to send -h, you can say:
send -- "-h"
The "--" does not affect strings that do not otherwise have a special meaning to send. Thus you can preface any string with it. For example, the following two commands both do exactly the same thing—they send the string foo.
send -- "foo" send "foo"
This behavior is particularly useful if you are sending the contents of a variable that can have any value.
send $unknown ;# DANGEROUS send -- $unknown ;# SAFE
Without the "--,”, it is possible that the unknown variable might contain something that looks like one of send’s special flags. Using the "--" guarantees that the command is interpreted correctly.
You might also use the "--" with literals when you are generating Expect commands from another program rather than writing them by hand. For example, imagine that you are writing a C program which in turn spits out Expect commands. Your code might look like this:
printf("send \"%s\"",unknown); /* DANGEROUS */
printf("send -- \"%s\"",unknown); /* SAFE */
While the resulting Expect script supplies the send command with literal values, the same problem arises. The C program is not checking to make sure that the value it is supplying to the send command does not look ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access