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?

fish shell scriptでbooleanを変数に入れる

0
Posted at

fish shellで変数にboolean値を入れる方法。

setコマンドを使えば文字列としてtrue/falseを格納できる。

boolean.fish
set foo true
# or
set foo false

文字列ではあるが、ifの条件式にそのまま突っ込むことでbooleanとして機能する。

if-condition.fish
set foo true
if $foo
  echo "実行されます!"
else
  echo "ここは実行されません。"
end

booleanを反転したい (not) なら頭に!をつければ良い。

if-condition.fish
set foo true
if ! $foo
  echo "ここは実行されません。"
else
  echo "実行されます!"
end

応用

コマンドの実行結果によって変数の値を変えたい場合は、ifを用いる。

if-advance.fish
# 任意の条件式を下に書く
if status is-interactive
  set foo true
else
  set foo false
end

# 変数の内容によって分岐する
if $foo
  echo "実行されます!"
else
  echo "ここは実行されません。"
end

若干分かりづらいが、変数代入を1行で書きたい場合は以下のようにする手もある。

one-line.fish
set is_no_echo (status is-interactive ; and echo false; or echo true)
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?