linux基础命令:cat和echo

一 cat

cat命令是Linux下的一个文本输出命令,通常是用于观看某个文件的内容的;
      cat主要有三大功能:
      1.一次显示整个文件。
      $ cat   filename
      2.从键盘创建一个文件。
      $ cat  >  filename
      只能创建新文件,不能编辑已有文件.
      3.将几个文件合并为一个文件。
      $cat   file1   file2  > file

[test@VM_0_15_centos ~]$ cat --help
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s), or standard input, to standard output.


-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit


-A, --show-all           等价于 -vET

-b, --number-nonblank    对非空输出行编号

-e                       等价于 -vE

-E, --show-ends          在每行结束处显示 $

-n, --number     对输出的所有行编号,由1开始对所有输出的行数编号

-s, --squeeze-blank  有连续两行以上的空白行,就代换为一行的空白行 

-t                       与 -vT 等价

-T, --show-tabs          将跳格字符显示为 ^I

-u                       (被忽略)

-v, --show-nonprinting   使用 ^ 和 M- 引用,除了 LFD 和 TAB 之外

 
范例:
cat  linuxfile1 > linuxfile2 把 linuxfile1 的档案内容加上行号后输入 linuxfile2 这个档案里,注意会覆盖Linuxfile2的内容
cat  linuxfile1 >> linuxfile2 把 linuxfile1追加加到linuxfile2,不覆盖Linuxfile2原有内容 

  cat >> oldboy.txt <<EOF
  1
  2
  3
  EOF

二 echo

用于字符串的输出

1.显示普通字符串:

[test@VM_0_15_centos ~]$ echo linux
linux

2.显示转义字符

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

3. 显示变量

[test@VM_0_15_centos ~]$ var=www
[test@VM_0_15_centos ~]$ echo  $var
www

4. 显示换行

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

5. 显示不换行

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

     删除前一个字符;

    c 最后不加上换行符号;

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

    
 换行且光标移至行首;

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

    	 插入tab;

    v 与f相同;

    \ 插入字符;

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

6. 显示结果定向到文件

[test@VM_0_15_centos ~]$ echo "hello world" >  text.txt
原文地址:https://www.cnblogs.com/wzy23/p/11376008.html