1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

.bash_aliasesでaliasを登録して管理する

Last updated at Posted at 2025-07-20

はじめに

Linuxのaliasは、よく使うコマンドや長いコマンドの組み合わせを短い名前のコマンドに設定する便利な機能で、.bashrcの設定ファイル(環境によって.zshrcなど)にaliasを登録して使います。
しかし、.bashrcファイルには、環境変数などの設定に関する定義が書かれていたり、設定が追記されるため、自分のalias設定の管理が難しくなる場合もあります。
そのため、.bashrcに直接aliasを登録せず、.bash_aliasesファイルでaliasを登録することを作成しておきたいと思います。

.bash_aliasesにaliasを登録

1. .bashrcの設定を確認する

まずは、.bash_aliasesファイルを使用するための設定が作成されているか確認します。
Ubuntuの場合は、基本的に以下のような設定が作成されていると思いますが、Linuxディストリビューションなどによって設定が書かれていないかもしれません。
そのように以下の設定がない場合は、.bashrcに設定を追記します。

.bashrc
# Alias definitions.
# You may want to put all your additions into a separate file like
# ~/.bash_aliases, instead of adding them here directly.
# See /usr/share/doc/bash-doc/examples in the bash-doc package.

if [ -f ~/.bash_aliases ]; then
    . ~/.bash_aliases
fi

2. .bash_aliasesを作成する

.bash_aliasesファイルにaliasを登録します。
.bash_aliasesファイルが存在しない場合は、新規作成します。

aliasは以下のような書き方で登録します。

alias登録
alias {名前}='{コマンド}'

以下は作成の例です

.bash_aliases
alias gs='git status'
alias gs='git add'
alias gc='git commit -m'

作成が完了したら、ファイルを保存し、以下のタスクを行って設定を反映します。

  • source ~/.bashrcコマンドを実行
  • source ~/.bash_aliasesコマンドを実行
  • ターミナルを再起動

3. aliasの登録確認

実際にコマンドを実行しても良いですが、以下のコマンドを実行すると、設定されたaliasの一覧が表示されます。
登録したaliasが一覧にあれば問題ないです。

$ alias

以上で.bash_aliasesのalias設定が完了になります。

おわりに

alias機能は長いそして複雑なコマンドを簡単に使用できるようにする便利な機能で、間違ったコマンドを実行することで起きる事故を予防するにも効果があると思います。
そのaliasを.bash_aliasesにまとめて登録しておけばより管理しやすくなると思います。

1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?