Echo (命令)

计算机技术中,echo是一个DOS、OS/2、Microsoft Windows、Singularity、Unix和类Unix系统中的命令,用来将字符串输出到终端上。它通常在shell脚本批处理中使用,以将状态信息输出到屏幕或文件中。

使用范例

$ echo This is a test.
This is a test.
$ echo "This is a test." > ./test.txt
$ cat ./test.txt
This is a test.

可以使用ANSI转义代码图形再现序列(SGR sequences)输出彩色文字:

FGRED=`echo "\033[31m"`
FGCYAN=`echo "\033[36m"`
BGRED=`echo "\033[41m"`
FGBLUE=`echo "\033[35m"`
BGGREEN=`echo "\033[42m"`
#将终端颜色恢复为正常状态
NORMAL=`echo "\033[m"`
#然后:
echo "${FGBLUE} Text in blue ${NORMAL}"
echo "Text normal"
echo "${BGRED} Background in red"
echo "${BGGREEN} Background in Green and back to Normal ${NORMAL}"

有的Unix变种(例如Linux)支持选项-n-e,且除非提供-e选项,否则不处理转义序列。例如,FGRED=`echo -e "\033[31m"` 可能会在Linux下使用。不幸的是,由于历史上BSD和System V的不兼容性,这不是标准选项[1];有问题时,可以使用printf命令。因此,建议使用printf,确保转义序列被正确处理。等效的printf代码就是FGRED=`printf "\033[31m"`

DOS、Microsoft Windows支持选项onoff。例如,echo off可以关闭回显,即不显示命令提示符。

C:\>ECHO off
::注意下一条命令前无命令提示符
ECHO Hello World!
Hello World!
ECHO on
C:\>

一般,批处理会在首行执行此条命令,以关闭执行过程中的回显。然而,执行此条命令亦会显示命令提示符,需使用@echo off以关闭该条命令的回显。

@ECHO off
ECHO Hello World!
PAUSE

实现范例

C语言中,echo命令可以用几行代码实现:

#include <stdlib.h>
#include <stdio.h>
/* echo command-line arguments; 1st version */
int main(int argc, char *argv[])
{
  int i;
  for (i = 1; i < argc-1; i++)
  {
      printf("%s%s", argv[i], " ");
  }
  printf("%s%s", argv[argc-1], "\n");
  return EXIT_SUCCESS;
}

Perl中的实现也很简单:

#!/usr/bin/env perl
print join " ", @ARGV;

参考

  1. ^ IEEE Std 1003.1, 2004, documentation for echo. [2013-04-15]. (原始内容存档于2010-04-01). 

外部链接

参见

文件系統
程序
  • at
  • bg
  • chroot
  • cron
  • fg
  • kill
  • killall英语killall
  • nice
  • pgrep
  • pkill英语pkill
  • ps
  • pstree英语pstree
  • time
  • top
用户环境
  • clear
  • env
  • exit
  • history英语history (Unix)
  • id
  • logname
  • mesg英语mesg
  • passwd
  • su
  • sudo
  • uptime
  • talk英语talk (software)
  • tput英语tput
  • uname
  • w
  • wall英语wall (Unix)
  • who
  • whoami
  • write英语write (Unix)
文本编辑
  • awk
  • banner英语banner (Unix)
  • basename
  • comm英语comm
  • csplit英语csplit
  • cut
  • diff
  • dirname
  • ed
  • ex
  • fmt英语fmt
  • fold英语fold (Unix)
  • head英语head (Unix)
  • iconv
  • join英语join (Unix)
  • less
  • more
  • nl英语nl (Unix)
  • paste英语paste (Unix)
  • sed
  • sort英语sort (Unix)
  • spell英语spell (Unix)
  • strings英语strings (Unix)
  • tail
  • tr
  • uniq英语uniq
  • vi
  • wc
  • xargs
壳层内建
网络英语Network utility
查找
文档
  • apropos英语apropos (Unix)
  • help英语help (command)
  • man
软件开发
杂项
  • bc
  • cal
  • dc
  • expr
  • lp英语lp (Unix)
  • lpr
  • sleep
  • true和false
  • yes
文件管理导航
文件管理操作
  • attrib英语ATTRIB
  • cacls
  • cipher
  • compact
  • copy
  • del英语del (command) (erase)
  • deltree英语deltree
  • expand
  • icacls
  • makecab
  • md (mkdir)
  • move英语move (command)
  • rd (rmdir)
  • recover英语recover (command)
  • ren英语ren (command) (rename)
  • replace英语replace (command)
  • Rmdir
  • robocopy英语robocopy
  • xcopy英语XCOPY
磁盘管理
  • chkdsk
  • convert英语convert (command)
  • defrag
  • diskcomp英语diskcomp
  • diskcopy英语diskcopy
  • diskpart英语diskpart
  • fdisk英语fdisk
  • format英语Format (command)
  • label英语label (command)
  • subst英语SUBST
  • scandisk
  • sys英语SYS (command)
  • vol英语vol (command)
  • vssadmin
进程
  • exit
  • kill
  • powercfg英语powercfg
  • runas
  • sc
  • shutdown
  • start英语start (command)
  • taskkill
  • tasklist英语tasklist
注册表
用户环境
  • date
  • mode
  • path
  • set
  • setx
  • time英语TIME (command)
  • title
  • ver英语ver (command)
  • Whoami
文件内容
  • comp英语comp (command)
  • edit英语MS-DOS Editor
  • edlin英语edlin
  • fc英语Microsoft File Compare
  • find英语find (command)
  • findstr
  • print英语PRINT (command)
  • type英语TYPE (DOS command)
脚本
  • choice英语choice (command)
  • clip
  • cscript
  • doskey英语DOSKEY
  • echo
  • for
  • Goto
  • if
  • more
  • pause
  • prompt英语Command-line_interface#Command_prompt
  • rem
网络
维护
启动管理
软件开发
杂项
  • cls
  • help英语help (command)