標籤:bash
bash的基礎特性
1、命令別名(alias)
通過alias命令實現:
(1) alias
顯示當前shell進程所有可用的命令別名;
(2) alias NAME=‘VALUE‘
定義別名NAME,其相當於執行命令VALUE;
注意:在命令列中定義的別名,僅對當前shell進程有效;如果想永久有效,要定義在設定檔中;
[[email protected] ~]# alias ssssssss=‘ls /‘ [[email protected] ~]# ssssssss bin data etc lib lost+found mnt proc sbin srv tmp var boot dev home lib64 media opt root selinux sys usr
僅對目前使用者:~/.bashrc
[[email protected] ~]# vim .bashrc # .bashrc # User specific aliases and functions alias rm=‘rm -i‘ alias cp=‘cp -i‘ alias mv=‘mv -i‘ # Source global definitions if [ -f /etc/bashrc ]; then . /etc/bashrc fi
對所有使用者有效:/etc/bashrc
Note: 編輯配置給出的新配置不會立即生效;
bash進程重新讀取設定檔:
source /path/to/config_file
. /path/to/config_file
source .bashrc
撤消別名:unalias
unalias [-a] name [name ...]
Note: 如果別名同原命令的名稱,則如果要執行原命令,可使用"\COMMAND";
2、glob (globbing)
bash中用於實現檔案名稱“通配”
萬用字元:*, ?, []
(1) *
任意長度的任一字元
a*b:
aab, ab, a123b,
abc
[[email protected] ~]# touch aaaa abc asddsc acccc
[[email protected] ~]# cp a*c test
[[email protected] ~]# ll test/total 48 -rw-r--r-- 1 root root 0 Jul 5 07:24 abc -rw-r--r-- 1 root root 0 Jul 5 07:24 acccc-rw-r--r-- 1 root root 0 Jul 5 07:24 asddsc-rw-r--r-- 1 root root 41433 May 21 2015 install.log-rw-r--r-- 1 root root 3422 May 24 2015 WorldCount.jar
(2) ?
任意單個字元
a?b:
aab
ab, a12b, abc
[[email protected] ~]# ll a*c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc -rw-r--r-- 1 root root 0 Jul 5 07:24 acccc -rw-r--r-- 1 root root 0 Jul 5 07:24 asddsc
[[email protected] ~]# cp a?c Music/
[[email protected] ~]# ll Music/ total 0 -rw-r--r-- 1 root root 0 Jul 5 07:26 abc
(3) []
匹配指定範圍內的任意單個字元
[0-9]
[a-z]:不區分字元大小寫
[[email protected] ~]# ll a*c -rw-r--r-- 1 root root 0 Jul 5 07:28 a2c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc -rw-r--r-- 1 root root 0 Jul 5 07:24 acccc -rw-r--r-- 1 root root 0 Jul 5 07:24 asddsc
[[email protected] ~]# ll a[0-9]c -rw-r--r-- 1 root root 0 Jul 5 07:28 a2c
[[email protected] ~]# ll a[a-z]c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc
[[email protected] ~]# ll a[A-Z]c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc
(4) [^]
匹配指定範圍外的任意單個字元
[^0-9] 除了數字
[[email protected] ~]# ll a*c -rw-r--r-- 1 root root 0 Jul 5 07:28 a2c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc -rw-r--r-- 1 root root 0 Jul 5 07:33 a%c -rw-r--r-- 1 root root 0 Jul 5 07:24 acccc -rw-r--r-- 1 root root 0 Jul 5 07:24 asddsc
[[email protected] ~]# ll a[^0-9]c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc -rw-r--r-- 1 root root 0 Jul 5 07:33 a%c
[[email protected] ~]# ll a[^a-z0-9]c -rw-r--r-- 1 root root 0 Jul 5 07:33 a%c
專用字元集合:
[:digit:]:任一數字,相當於0-9
[:lower:]:任意小寫字母
[[email protected] ~]# ll a*c -rw-r--r-- 1 root root 0 Jul 5 07:28 a2c -rw-r--r-- 1 root root 0 Jul 5 07:37 aAc -rw-r--r-- 1 root root 0 Jul 5 07:24 abc -rw-r--r-- 1 root root 0 Jul 5 07:33 a%c -rw-r--r-- 1 root root 0 Jul 5 07:24 acccc -rw-r--r-- 1 root root 0 Jul 5 07:24 asddsc
[[email protected] ~]# ll a[[:lower:]]c -rw-r--r-- 1 root root 0 Jul 5 07:24 abc
[:upper:]: 任意大寫字母
[:alpha:]: 任意大小寫字母
[:alnum:]:任一數字或字母
[:space:]:
[:punct:]:標點符號
練習:
1、顯示/var目錄下所有以l開頭,以一個小寫字母結尾,且中間出現至少一位元字的檔案或目錄;
ls -d /var/l*[0-9]*[[:lower:]]
2、顯示/etc目錄下,以任意一位元字開頭,且以非數字結尾的檔案或目錄;
ls -d /etc/[0-9]*[^0-9]
3、顯示/etc/目錄下,以非字母開頭,後面跟了一個字母及其它任意長度任一字元的檔案或目錄;
ls /etc/[^[:alpha:]][[:alpha:]]*
4、複製/etc目錄下,所有以m開頭,以非數字結尾的檔案或目錄至/tmp/mageedu目錄中;
cp -a /etc/m*[^0-9] /tmp/mageedu
5、複製/etc目錄下,所有以.d結尾的檔案或目錄至/tmp/magedu.com目錄中;
cp -a /etc/*.d /tmp/magedu.com
6、複製/etc目錄下,所以有.conf結尾,且以m,n,r,p開頭的檔案或目錄至/tmp/mageedu.com目錄中;
cp -a /etc/[mnrp]*.conf /tmp/mageedu.com
3、bash的快速鍵
Ctrl+l: 清屏,相當於clear命令;
Ctrl+a:跳轉至命令開始處;
Ctrl+e:跳轉至命令結尾處;
Ctrl+c:取消命令的執行;
Ctrl+u: 刪除命令列首至光村所在處的所有內容;
Ctrl+k: 刪除游標所在處至命令列尾部的所有內容;
總結:寫得不好,有錯誤請指出!謝謝!
本文出自 “梁小明的部落格” 部落格,請務必保留此出處http://7038006.blog.51cto.com/7028006/1812426
bash的基礎特性