文字列の操作
参考:
Bash string processing | Green Flashes
目次
文字列を連結する
パラメータ展開を使う。
hello="Hello" world="World" hello_world="$hello, $world" echo "$hello_world"
printf
を使う。
hello="Hello" world="World" hello_world=$( printf "%s, %s" "$hello" "$world" )
+=
を使う。
str="Hello" str+=", World"
参考:
bash の文字列の追記 (append) 演算子の紹介 | それマグで!
Bash String Concatenation | nixCraft
How To Bash Concatenate or Add Strings? – POFTUT
How to Concatenate two string variables in Bash Script | TecAdmin.net
How to concatenate string variables in Bash – Stack Overflow
how to concatenate string variables into a third? – Unix & Linux Stack Exchange
how to concatenate the following strings? – Ask Ubuntu
How do I concatenate strings in a bash script? – Super User
How does one concatenate strings in Bash? – Quora
文字列をエスケープ処理する
escaped=$( printf "%q" "$str" )
$ echo $( printf "%q" '"$hello, $world\n"' ) \"\$hello\,\ \$world\\n\"
参考:
ssh などの引数がコマンドとして解釈されるコマンドに文字列を渡す – Qiita
Quoting and Escaping Part 1 | Cloud 66 blog
How can I escape double quotes in content gotten from file – Server Fault
ヌルストリング
空文字と値なしは同じ意味
var="" var='' var=
参考:
Are the null string and “” the same string? – Unix & Linux Stack Exchange
指定の幅に文字を埋める
参考:
How to pad strings with spaces in a Unix shell script? – Super User
pad end of each line with spaces to = 80 chars ?? – LinuxQuestions.org
前後の空白を取り除く
echo
を使う。
$ echo $(cat file.txt | wc -l)
参考:
シェルス変数の先頭にあるスペースの除去 | My life is programming
正規表現に一致する部分を取り出す
expr
コマンドを使う。
参考:
Unicode 文字の ANSI-C クォーティング
\u
に4桁のコードポイントを続けて Unicode 文字を表す。
$ echo -e "\u263B"
\U
に8桁のコードポイントを続けて Unicode 文字を表す。
$ echo -e "\U0001f603"
参考:
Bash 対応の Unicode コードポイント表記を出力するシェル芸 | Ryoto’s Blog
シェル上で Unicode 絵文字を表示させる – Qiita
ANSI-C Quoting | Bash Reference Manual
How do you echo a 4-digit Unicode character in Bash? – Stack Overflow
Tips
参考: