linux基礎命令 cat和echo

2022-06-29 18:21:12 字數 2793 閱讀 3614

一 cat

cat命令是linux下的乙個文字輸出命令,通常是用於**某個檔案的內容的;

cat主要有三大功能:

1.一次顯示整個檔案。

$ catfilename

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 <二 echo

[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! \n

" # -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 發出警告聲;

\b 刪除前乙個字元;

\c 最後不加上換行符號;

\f 換行但游標仍舊停留在原來的位置;

\n 換行且游標移至行首;

\r 游標移至行首,但不換行;

\t 插入tab;

\v 與\f相同;

\\ 插入\字元;

\nnn 插入nnn(八進位制)所代表的ascii字元;

6. 顯示結果定向到檔案

[test@vm_0_15_centos ~]$ echo

"hello world

" > text.txt

Linux基礎命令 cat

cat 連線文字檔案或者標準輸入,將結果輸出到標準輸出裝置。此命令的適用範圍 redhat rhel ubuntu centos suse opensuse fedora。1 語法 cat 選項 file cat file1 file2 file3 2 選項列表 選項說明 help 顯示幫助文件 v...

linux命令之cat命令

cat命令的用途是連線檔案,列印檔案內容 它的三大功能有 標準化輸出檔案 cat filename 建立檔案但不能編輯 cat filename 將幾個檔案合併為乙個檔案 cat file1 file2 file 主要引數 a,show all 等價於 vet b,number nonblank 對...

linux命令之 Cat命令

cat命令 作用 cat命令用於連線檔案並列印,檢視檔案內容 n或 number 由1開始對所有輸出的行數編號 b或 number nonblank 和 n相似,只不過對於空白行不做編號 s或 squeeze blank 當遇到有連續兩行以上的空白行,就代換為一行的空白行 v或 show nonpr...