Quantcast
Channel: Bashタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 2822

【bash】文字列比較で、スペースを文字としてカウントしない方法

$
0
0

どういうこと?

bashにおいて、スペースのみを変数に代入した時も文字有りと判断される。

string=' 'test-n"${string}"&&echo yes# 比較演算子 n は1文字以上かを判定する。結果は yes

どうしたいのか?

スペースだけで構成される文字列を空(文字数0)と判定して欲しい。

どうやるのか?

tr コマンドでスペースを除去した状態で、判定する、だけ。

test-n$(echo"${string}" | tr-d" ")||echo no
#結果は no

Viewing all articles
Browse latest Browse all 2822

Latest Images

Trending Articles