1

I'm trying to put a pipe in a variable. However, i can't seem to.

If i do set elfheader=|ELF, i just get:

"'ELF' is not recognized as an internal or external command, operable program or batch file."

This is beacuse batch executes things literally.

I've tried something like:

set elfheader=(
|ELF
)

and it doesn't work.

I know why this happens, it's beacuse batch executes things literally and does this (however that is not the question i am asking):

"okay, oh, we want to set a variable. currently (... oh, pipe. so set that to ( and let's go execute the next command. hmm... what is "ELF"? what?"

TL;DR/To clarify, i am trying to put "|ELF" in a variable, in the batch programming language. However, it fails.

EDIT: This is not to clarify what can be used. I want to put that in a variable, not know what can not be used. I want to avoid that.

180Five
  • 13
  • 5
  • 1
    Possible duplicate of [What are vaild characters for environment variable names and values?](http://stackoverflow.com/questions/20635695/what-are-vaild-characters-for-environment-variable-names-and-values) – NineBerry Dec 04 '16 at 18:49
  • @NineBerry Not that question, sorry. – 180Five Dec 04 '16 at 19:04
  • Yes, the answer is there. Use ^ as an escape character – NineBerry Dec 04 '16 at 19:05
  • @NineBerry I know, but the question there is not the question i am asking. And OP never asked "how do i avoid it?". It is just additional information, however this should be classified as a separate question. – 180Five Dec 05 '16 at 08:48

1 Answers1

2

Supposing you want to assign the pipe character | to a variable, there are two options:

  1. To use quotation marks around the entire assignment expression:

    set "elfheader=|ELF"
    

    Note that the quotation marks do not become part of the value. Let me recommend this method, because it avoids spacial handling of also other special characters; in addition, this avoids unintended trailing spaces to be appended in case there are some after the second ". However, this syntax only works in case the command extensions are enabled, which is the default anyway.

  2. To use escaping by the character ^:

    set elfheader=^|ELF
    

    This works also with command extensions disabled (which is rarely the case though), but you need to take care for every single special character, like ^, &, <, >, |, and when the command line is placed within a parenthesised block of code, also (, ), and when delayed expansion is enabled, also !.

aschipfl
  • 33,626
  • 12
  • 54
  • 99