9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

bash read コマンドを利用する場合には、基本的に -r オプションを使うべき。

Last updated at Posted at 2015-12-02

read コマンドは、 \ があると基本的にそれを無視してしまう。

(read line <<< 'hello\nworld'; echo $line)
# => hellonworld

-r オプションを付与すると、バックスラッシュの特殊な挙動を抑制し、ただの文字として扱うことができる。

(read -r line <<< 'hello\nworld'; echo $line)
# => hello\nworld

-r オプションで抑制される特殊な挙動は何があるかというと、 line continuation と、IFSの抑制がある模様。

(read line <<EOF; echo $line)
hello \
world
EOF
# => hello world

(read x y <<< 'foo\ bar baz'; echo "<$x><$y>")
# => <foo bar><baz>

参考: http://wiki.bash-hackers.org/commands/builtin/read

9
8
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
9
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?