LoginSignup
10
10

More than 3 years have passed since last update.

ShellScript 変数内 の 文字列 を 置換

Posted at

目的

  • 変数に格納された文字列の中からマッチした文字列を任意文字列に置換する方法を知る。

書き方の例

  • 変数には任意の文字列が格納されているものとする。
  • 下記にShellScriptの処理を記載する。
$ echo ${変数//置換したい文字/置換後の文字}

より具体的な例

  • 変数stringには文字列「aaabbbcccddd」が格納される。
  • 文字「a」を、文字「e」に置換する。
  • 下記にShellScriptの処理を記載する。
# 変数stringに文字列aaabbbcccdddを格納
$ string="aaabbbcccddd"

# 変数stringに格納された文字列のaをeに置換し、結果を変数stringに格納
$ string=`echo ${string//a/e}`

# 変数stringを出力
$ echo ${string}
>eeebbbcccddd
10
10
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
10
10