Detailed explanation of Linux echo command

The echo command of linux is very commonly used in shell programming. It is also often used when printing variable values ​​under the terminal. Therefore, it is necessary to understand the usage of

echo. The function of the echo command is to display a piece of text on the display, which generally acts as a The role of prompting.
The general format of this command is: echo [ -n ] string
The option n means that the output text will not be newline; the string can be quoted or not. When using the echo command to output a quoted string, the string is output as it is; when using the echo command to output an unquoted string, each word in the string is output as a string, and each string is separated by a space .

Function description: Display text.
Syntax: echo [-ne][string] or echo [--help][--version]
Additional description: echo will send the input string to standard output. The output strings are separated by whitespace characters and appended with a newline at the end.
Parameters: -n Don't wrap the line at the end
-e If the following characters appear in the string, treat them specially instead of treating them as normal

  1. # echo [Options] [String]

Items in square brackets are optional. A string can be defined as a finite sequence of characters (such as letters, numbers, symbols, punctuation).

When the echo command is used without any options or strings, it returns a blank line on the display followed by a new line to follow the command prompt. This is because pressing the enter key sends a signal to the system to start a new line, and echo repeats that signal.

Options:

  • -n do not output derived newlines
  • -e enable backslash escape interpretation
  • -E disable backslash escape interpretation (default)

If you use the -e option, you can use escape sequences like this:

  • \ backslash
  • \a Warning (BEL)
  • \b backslash
  • \c produces no further output
  • \e escape
  • \f form feed
  • \n new line
  • \r newline character
  • \t horizontal tab
  • \v vertical tab
  • \0NNN Octal value representation of byte NNN (1 to 3 digits)
  • \xHH Byte NNN (1 to 2 digits) represented by hexadecimal value

不同应用场景的10个Linux面试问题与解答 http://www.linuxidc.com/Linux/2014-04/99710.htm

10个核心的Linux面试问题与答案 http://www.linuxidc.com/Linux/2014-04/100447.htm

2.显示转义字符

echo "\"It is a test\""
结果将是:
"It is a test"
同样,双引号也可以省略

 

3.显示变量

read 命令从标准输入中读取一行,并把输入行的每个字段的值指定给 shell 变量

#!/bin/sh
read name 
echo "$name It is a test"

 

以上代码保存为 test.sh,name 接收标准输入的变量,结果将是:

[root@www ~]# sh test.sh
OK                     #标准输入
OK It is a test        #输出

 

4.显示换行

echo -e "OK! \n" # -e 开启转义
echo "It it a test"

 

输出结果:

OK!

It it a test

 

5.显示不换行

#!/bin/sh
echo -e "OK! \c" # -e 开启转义 \c 不换行
echo "It is a test"
输出结果:OK! It is a test

 

 
复制代码
  • 转义

    \a 发出警告声;

    \b 删除前一个字符;

    \c 最后不加上换行符号;

    \f 换行但光标仍旧停留在原来的位置;

    \n 换行且光标移至行首;

    \r 光标移至行首,但不换行;

    \t 插入tab;

    \v 与\f相同;

    \\ 插入\字符;

    \nnn 插入nnn(八进制)所代表的ASCII字符;

复制代码

 

 

6.显示结果定向至文件

echo "It is a test" > myfile

 

7.原样输出字符串,不进行转义或取变量(用单引号)

echo '$name\"'
输出结果:
$name\"

 

8.显示命令执行结果

  echo `date`
结果将显示当前日期

Thu Jul 24 10:08:46 CST 201

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325549370&siteId=291194637