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

More than 1 year has passed since last update.

【Shell】コマンドの実行結果を画面に出力した上でパイプに渡す方法

Last updated at Posted at 2023-04-03

はじめに

コマンドの結果をパイプで渡すと以下のようになります。

$ ls -a | grep .env
.env

上記のls -aの実行結果も画面に表示させる方法について記載します。

画面にも表示させる方法

コマンドの実行結果を画面とパイプの両方に渡すには以下のように実行します。

zsh
$ ls -a >&2 | grep .env
.
..
.DS_Store
.env
.git
.env

上記の実行結果の内上から.gitまでがls -aの実行結果です。
最後に再度.envと表示されているのがgrep .envの実行結果になります。

上記の方法はshやbashでは使用できません。

bash
$ ls -a >&2 | grep .env
.           
..
.DS_Store
.env
.git

$ echo $?
1

以下の方法はzshとbashで使用することができます。

zshまたはbash
$ ls -a | tee >(grep .env)
.
..
.DS_Store
.env
.git
.env

shで実行すると以下のようにエラーとなります。

sh
$ ls -a | tee >(grep .env)
sh: 1: Syntax error: "(" unexpected

以下の方法であればshでも使用可能です。

$ ls -a | tee -a /dev/stderr | grep .env
.
..
.DS_Store
.env
.git
.env
0
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
0
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?