Linux Bash Shell命令别名介绍

运维 系统运维
命令别名是一个很有趣的东西,特别是你的惯用指令特别长的时候!还有,预防一些不小心误杀档案的情况发生的时候!

Linux Bash Shell命令别名是一个很有趣的东西,特别是你的惯用指令特别长的时候!还有,预防一些不小心误杀档案的情况发生的时候!举个例子来说,如果你要查询隐藏档,并且需要长的列出与一页一页翻看,那么需要下达『 ls -al | more 』这个指令,我是觉得很烦啦!要输入好几个单字!那可不可以使用 lm 来简化呢?!当然可以,你可以在命令列下面下达:

[test @tset test]# alias lm='ls -al | more'

要注意的是:『alias 的定义规则与变数定义规则几乎相同』,所以你只要在 alias 后面加上你的{『别名』='指令 参数' },以后你只要输入 lm 就相当于输入了 ls -al|more 这一串指令!很方便吧!另外,我们知道 root 可以移除( rm )任何资料!所以当你以 root 的身份在进行工作时,需要特别小心,但是总有失手的时候,那么 rm 提供了一个参数来让我们确认是否要移除该档案,那就是 -i 这个参数!所以,你可以这样做:

[test @tset test]# alias rm='rm -i'
嘿嘿!那么以后使用 rm 的时候,就不用太担心会有错误删除的情况了!这也是命令别名的优点啰!那么如何知道目前有哪些的命令别名呢?就使用 alias 呀!
 

  1. [test @tset test]# alias   
  2. alias l.='ls -d .[a-zA-Z]* --color=tty'   
  3. alias ll='ls -l'   
  4. alias lm='ls -al'   
  5. alias ls='ls --color=tty'   
  6. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' 

· 至于如果要取消命令别名的话,那么就使用 unalias 吧!
· 历史指令记录资料:
前面我们提过 Linux Bash Shell有提供指令历史的服务!那么如何查询我们曾经下达过的指令呢?就使用 history 啰!
o history, !command
显示历史指令记录内容, 下达历史纪录中的指令
Linux Bash Shell语法:
 
 

  1. [test @tset test]# alias   
  2. alias l.='ls -d .[a-zA-Z]* --color=tty'   
  3. alias ll='ls -l'   
  4. alias lm='ls -al'   
  5. alias ls='ls --color=tty'   
  6. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde' 

Linux Bash Shell说明:
基本上 history 的用途很大的!但是需要小心安全的问题!尤其是 root 的历史纪录档案,这是 Cracker 的最爱!因为不小心的 root 会将很多的重要资料在执行的过程中会被纪录在 ~/.bash_history 当中,如果这个档案被解析的话.....而使用『 ! 』配合曾经使用过的指令下达是很有效率的一个指令方法!
鸟哥的常用的个人喜好设定值:
底下是 VBird 最喜欢的设定值啰!大家可以随意的参考看看就好了!
 

  1. [test @test test]# vi .bashrc   
  2. # .bashrc   
  3. # User specific aliases and functions   
  4. PATH="/usr/local/bin:/usr/local/sbin:/bin:/sbin:/usr/sbin:/usr/bin:$PATH" <==常用!   
  5. export PATH   
  6. alias rm='rm -i'   
  7. alias cp='cp -i'   
  8. alias mv='mv -i'   
  9. alias ll='ls -l'   
  10. alias lm='ls -al|more' <==常用!   
  11. alias h='history'<==常用!   
  12. # Source global definitions   
  13. if [ -f /etc/bashrc ]; then   
  14. . /etc/bashrc   
  15. fi   

[test @test test]# source ~/.bashrc<==將剛剛改的資料直接讀入這一次的程序當中!不需登出!

当然还有一些个人比较常用的变数,这里就不列出来了!那么改写完毕之后。此外,通常我们改写的这些设定,必须要登出再登入才能被启用!但是我们可以使用 source 来直接的启用他!这也是很多朋友为了因应不同的软体或者是其他的执行环境,而写了不同的环境设定档案(不一定是 .bashrc 这个档案),然后在需要的时候再以 source 来将设定读出来即可!这个指令可也是相当重要的呦!

【编辑推荐】

  1. Linux Bash命令关于程序调试详解 
  2. Linux Bash命令关于快捷键应用
  3. Linux Bash Shell系统的应用详解
  4. Linux Bash具体安装步骤及使用介绍
  5. Linux Bash命令查询相关事件详解

 

责任编辑:chenqingxiang 来源: Linux社区
相关推荐

2010-06-23 16:31:10

Linux Bash

2010-06-23 17:34:03

Linux Bash

2010-06-23 17:37:14

Linux Bash

2010-06-23 17:29:07

Linux Bash

2018-01-16 10:08:25

Linuxbashshell

2023-07-27 17:41:02

BashZshFish

2010-06-23 16:05:36

Linux Bash

2010-06-23 15:55:36

Linux Bash

2010-06-23 14:45:02

Linux Bash

2009-12-25 09:47:05

LinuxShell编程bash

2010-06-23 16:42:33

2010-06-23 16:09:40

Linux Bash

2018-10-12 10:40:45

LinuxBash命令

2010-06-23 16:35:50

Linux Bash

2010-03-23 15:52:41

Linux shell

2023-10-31 16:17:42

2010-06-23 15:36:23

Linux Bug B

2009-12-18 08:57:27

Linux shell

2019-08-05 10:00:13

LinuxBash命令

2018-01-22 09:12:18

Linuxbashssh
点赞
收藏

51CTO技术栈公众号