shell概述與echo命令

2022-01-10 08:07:41 字數 2315 閱讀 5837

一、shell概述

1、什麼是shell?

由此可見shell是使用者與系統進行互動的介面,通過執行各種命令來完成時間處理與排程。

2、shell分類

shell類別                易學性        可移植性      編輯性      快捷性 

bourne shell (sh) 容易 好 較差 較差

korn shell (ksh) 較難 較好 好 較好

bourne again (bash) 難 較好 好 好

posix shell (psh) 較難 好 好 較好

c shell (csh) 較難 差 較好 較好

tc shell (tcsh) 難 差 好 好

shell 的兩種主要語法型別有 bourne 和 c,這兩種語法彼此不相容。

bourne 家族主要包括 sh、 ksh、bash、psh、zsh。

c 家族主要包括:csh、tcsh (bash 和 zsh 在不同程度上支援 csh 的語法)。

我們可以通過/etc/shells 檔案來查詢 linux 支援的 shell。

[root@centos ~]# cat /etc/shells 

/bin/sh

/bin/bash

/sbin/nologin

/bin/dash

/bin/tcsh

/bin/csh

二、echo命令及shell指令碼執行

1、echo 輸出特殊轉義字元

echo [選項] [輸出內容] 選項:  

-e: 支援反斜線控制的字元轉換

\\ 輸出\本身

\a 輸出警告音

\b 退格鍵,也就是向左刪除鍵

\c 取消輸出行末的換行符

\e escape 鍵

\f 換頁符

\n 換行符

\r 回車鍵

\t 製表符,也就是 tab 鍵

\v 垂直製表符

\0nnn 按照八進位制 ascii 碼表輸出字元。其中 0 為數字零,nnn 是三位八進位制數

\xhh 按照十六進製制 ascii 碼表輸出字元。其中 hh 是兩位十六進製制數

-n: 取消輸出後行末的換行符號(就是內容輸出後不換行)

示例

[root@centos ~]# echo -e "heihei \nhaha \a" #先輸出heihei,換行之後再輸出乙個 haha 最後輸出警示音

heihei

haha

2、echo輸出顏色
30m=黑色,31m=紅色, 32m=綠色,33m=黃色,34m=藍色,35m=洋紅,36m=青色,37m=白色

1)字型顏色

echo -e "\e[1;32m 綠色字型 \e[0m"

2)背景顏色

echo -e "\e[1;42m 綠色背景 \e[0m"

3)閃爍

echo -e "\e[1;5m \e[1;32m 綠色閃爍字型 \e[0m \e[0m "

3、執行shell指令碼

執行shell指令碼有兩種方式,下面是乙個簡單的shell指令碼:

#!/bin/bash   #直接執行時,告訴系統應該用哪乙個直譯器來執行。

echo -e "\e[1;5m \e[1;32m 你好!! \e[0m "\e[0m

[root@centos ~]# chmod a+x test.sh 

[root@centos ~]# . test.sh

你好![root@centos ~]# /root/test.sh

你好!

[root@centos ~]# bash test.sh 

你好!

shell命令之echo命令詳解

帶雙引號不帶雙引號輸出的結果一致,雙引號可省略 echo it is a test echo it is a testecho it is a test 輸出如下 it is a test 能否引用變數 能否引用轉移符 能否引用文字格式符 如 換行符 製表符 單引號否 否雙引號能能 無引號能能 補充...

shell中的echo命令

echo 是乙個 shell 內建命令,用來在終端輸出字串 root server1 mnt sh test.sh students,你好!root server1 mnt cat test.sh bin bash name shell教程 url echo students,你好!直接輸出字串 e...

shell指令碼 echo命令使用

這裡直接給出例子 顯示普通字串 echo it is a test 這裡的雙引號完全可以省略,以下命令與上面例項效果一致 echo it is a test 顯示轉義字串 echo it is a test 顯示結果 it is a test 顯示變數 read 命令從標準輸入中讀取一行,並把輸入行...