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?

備忘録 〜シェルの変数、計算式の書式まとめ(随時更新)

Posted at

シェルの変数、計算絡みの書式についてメモ。
学習環境はbash(たまにzsh)です。

学習メモにつき、誤り等ありましたら、ぜひご指摘お願いします。

変数

代入 number=1

  • 「=」の前後にスペースを入れない
    (スペースで区切るとコマンドの引数とみなされるから)

計算式

echo $((5+3))または
expr 5 + 3

exprコマンドについて

  • 計算し、結果を標準出力に出すコマンド。
  • 各数値、演算子の間にはスペースが必要(あくまでコマンドの引数だから
  • 掛け算(*)の場合には要エスケープ(コマンドでの「*」は特殊文字だから)
  • 例: expr 5 \* 3
  • 同様に、「 ( 」や「 ) 」についてもエスケープが必要
    例:expr \( 5 + 3 \) \* 8

コマンド置換

コマンド結果を文字列に変換して、他のコマンドの一部(引数としても可)で使える。
echo "今日は$(date)"

  • 「"」をスラッシュ「'」にすると置換されなくなる
  • 「$date」は「'date'」でも可

まとめ

スペースの有無はややこしいですが、「何故必要なのか」「なぜ有ってはならないのか」を理解しておくと、忘れなさそうです。

参考

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?