LoginSignup
4
4

More than 5 years have passed since last update.

xargsでzshの自作シェル関数を使う

Last updated at Posted at 2016-02-26

zsh (少なくともver.5.0.7)ではxargsコマンドで自作シェル関数が使えない。
.zshrcでmyfuncという関数を定義している場合、

$ ls hoge | xargs myfunc

を実行すると
xargs: myfunc: そのようなファイルやディレクトリはありません
となる。

そこでxargsを実行する際にドットコマンドを用いてmyfunc関数が定義してある.zshrcを一回ずつ一々読み込むようにする。

$ ls hoge | xargs -I{} zsh -c ". ~/.zshrc; myfunc {}"

ここで
-I{} zsh -c ". ~/.zshrc; myfunc {}"
はxargsで複数のコマンドを実行するやり方で、実行される度に. ~/.zshrcと標準出力の引数を{}に渡しmyfuncが実行される。

bashではexport -fを使えばこのようなことをしなくても済むっぽい。
http://d.hatena.ne.jp/wristed/20120603/1338691396
ただしここで紹介したやり方ではbashでも使えると思う。

4
4
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
4
4