LoginSignup
10
8

More than 5 years have passed since last update.

Bash環境でエイリアス(alias)の定義をしよう

Last updated at Posted at 2019-01-01

はじめに

Bash環境でエイリアス(alias)を定義、一覧表示、削除してみよう。

エイリアスの定義

一時的にエイリアスを定義する

alias name='value'コマンドで、エイリアスを定義します。

$ alias pshttpd='ps aux | grep httpd'

定義したエイリアス名を入力すると、ジョブが実行されます。

$ pshttpd
papapap+  7403  0.0  0.1 112668   976 pts/0    R+   15:43   0:00 grep --color=auto httpd
root     28361  0.0  2.9 433368 14672 ?        Ss    2018   8:02 /usr/sbin/httpd -DFOREGROUND
apache   31824  0.0  1.8 435588  9140 ?        S     2018   0:00 /usr/sbin/httpd -DFOREGROUND
apache   31825  0.0  1.8 435588  9140 ?        S     2018   0:00 /usr/sbin/httpd -DFOREGROUND
apache   31826  0.0  1.7 435588  8912 ?        S     2018   0:00 /usr/sbin/httpd -DFOREGROUND
apache   31827  0.0  1.7 435588  8912 ?        S     2018   0:00 /usr/sbin/httpd -DFOREGROUND
apache   31828  0.0  1.8 435588  9140 ?        S     2018   0:00 /usr/sbin/httpd -DFOREGROUND
apache   32093  0.0  1.8 435588  9136 ?        S     2018   0:00 /usr/sbin/httpd -DFOREGROUND

恒久的にエイリアスを定義する

aliasコマンドでエイリアスを定義しただけでは、次回ログイン時にはエイリアスが削除されてしまいます。
恒久的にエイリアスを定義したい場合は、 ~/.bash_profileファイルや~/.bashrcファイルにalias name='value'を記載します。

~/.bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
    . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
export paper=papapapaper

alias datemin="date '+%y%m%d'"

alias name='value'を記載した後は、$ source .bash_profileコマンドでファイルの読み込みを行います。

$ source .bash_profile

定義したエイリアス名を入力すると、ジョブが実行されます。

$ datemin
190101

エイリアスの一覧表示

alias, alias -pコマンドでエイリアスの一覧を表示できます。

$ alias
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias pshttpd='ps aux | grep httpd'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
$ alias -p
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias pshttpd='ps aux | grep httpd'
alias vi='vim'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

エイリアスの削除

unalias nameコマンドで、エイリアスを削除します。

$ unalias datemin

恒久的に定義したエイリアスは、unaliasコマンドで削除しても次回ログイン時に復活します。
 ~/.bash_profileファイルや~/.bashrcファイルに記載したalias name='value'を削除してください。

10
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8