0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Shell】変数が空または未定義の場合のデフォルト値を設定する方法

Posted at

:-を使用して空文字か未定義の場合のデフォルト値を設定できます。

hoge=Hello
foo=""

echo ${hoge:-exist}
# Hello
echo ${foo:-empty}
# empty
echo ${bar:-undefined}
# undefined

変数が定義されていない場合のみデフォルト値とする場合には-を使用します。

hoge=Hello
foo=""

echo ${hoge-exist}
# Hello
echo ${foo-empty}
#
echo ${bar-undefined}
# undefined
0
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?