如何使Windows CMD ECHO恰好回显单个字符? [英] How to get Windows CMD ECHO to echo exactly one single character?
问题描述
Windows Internal CMD ECHO
的(非正式)文档显示了一些有趣的技巧它.但是,我还没有找到回声单个字符的方法.
快速注释,下面使用的 od
来自Git(或Gow)安装
Quick note, od
used below, is from a Git (or Gow) installation
例如,此回显是带有"windows"换行符(\r\n
)的'a'
:
For instance, this echo's the 'a'
with a 'windows' newline (\r\n
):
>echo a| od -A x -t x1z -v -
000000 61 0d 0a >a..<
000003
技巧(也在现在的文档中)echo什么也没有:
And this trick (also in the docs now) echo's nothing:
><nul (set/p _any_variable=)| od -A x -t x1z -v -
000000
因此,我希望它仅回显'a':
So I would expect this to echo just the 'a':
><nul (set/p _any_variable=a)| od -A x -t x1z -v -
000000 61 20 >a <
000002
但是它最后增加了额外的空间.
是否可以只做一个字符?
Is it possible to just do a single character?
@Aacini在下面的评论中正确回答了(第一个问题),但是如果他没有创建答案,则为:
@Aacini answered (the first question) correctly in a comment below, but in case he does not create an answer, here it is:
>set /P "=a" < NUL | od -A x -t x1z -v -
000000 61 >a<
000001
还有什么技巧可以使它更精确,例如UNIX echo
和-n
(无新行)和-e
(使用反斜杠解释),因此我可以将输出与此类似:
And are there any tricks to get more precise like the UNIX echo
with a -n
(no new line) and -e
(use backslash interpretation) so I could similar outputs to this:
>unix_echo -n -e "a\n" | od -A x -t x1z -v -
000000 61 0a >a.<
000002
推荐答案
set /P
命令用于提示用户并接受输入.例如:
The set /P
command is used to prompt the user and accept an input. For example:
set /P "name=Enter your name: "
此命令显示提示信息,并将光标置于其后.我们可能会很好地利用此行为来显示提示",该提示在CR + LF中没有 not 结尾,然后完成将Stdin重定向到NUL的虚拟输入.在这种情况下,不需要变量名称:
This command show the prompt message and place the cursor after it. We may make good use of this behavior to show a "prompt" that does not end in CR+LF, and then complete the dummy input redirecting Stdin to NUL. In this case, the variabe name is not needed:
set /P "=Text with no CR+LF at end" < NUL
这样,仅输出一个字符,请使用以下命令:
This way, to output just one character, use this:
set /P "=a" < NUL
请注意,set /P
命令会在提示消息中省略任何前导空格.这意味着不可能使用此方法仅显示空格.
Note that set /P
command omit any leading space from the prompt message. This means that it is not possible to use this method to show only spaces.
这篇关于如何使Windows CMD ECHO恰好回显单个字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!