LoginSignup
5
1

More than 5 years have passed since last update.

bashのaliasに引数を渡したい

Last updated at Posted at 2018-02-21

目的

発端はssh終了後に明確に抜けたことを表示させるためclearがしたかった。

  • やりたいこと ssh remote-host && clear

管理するInstanceが多い場合や、ssh-completionで毎回clearを打つのも億劫だったので方法を調べた。

aliasではうまくいかない

alias だとoptionsと引数の順番は指定できない。
解決策としてfunctionか、適当なコマンドファイル(例えばssh.sh)を作る。
今回はコマンドファイルを作成する。

コマンドファイル

パスの通ったディレクトリーに、以下のファイルを作成する。

ssh.sh
#!/usr/bin/env bash
ssh $@ && clear

さらに、今回は標準コマンドのsshの名称を使いたいので、aliasで追加する。
~/.bashrc~/.bash_profileに以下を追記。
aliasが利用出来るので任意の名称が使えますのでコマンドと同じscript名に抵抗がある場合は適宜置き換えて下さい。

.bash_profile
alias ssh="~/.sh/ssh.sh"

参考

bashのalias に引数を渡すには? - それマグで!

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