1
4

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 3 years have passed since last update.

シェルスクリプトのコマンド置換

Posted at

#コマンド置換
シェルスクリプトを書いていると、コマンドの出力結果をシェルスクリプト中で利用したいことがある。
コマンド置換を使えば、コマンドの結果を文字列として取得することができる。

##利用方法
$()という形式で、カッコ内に実行したいコマンドを記述すると、シェルスクリプト実行時に標準出力で置き換えられる。

##例:現在の日付をYYYY-MM-DD形式で表示

.sh
$ date '+%Y-%m-%d'
2020-05-04

###コマンド置換で利用する

date.sh
#!/bin/bash

filename=$(date '+%Y-%m-%d')
touch "$filename"

これを実行すると、現在の日付をYYYY-MM-DD形式で表示したファイルが作成される

-rw-rw-r--  1 vagrant vagrant    0 May  4 10:54 2020-05-04

##参考
新しいLinuxの教科書

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?