LoginSignup
8
8

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

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