mirror of
https://github.com/koalaman/shellcheck.git
synced 2025-10-03 19:29:44 +08:00
Call out IFS= more prominently.
15
SC2162.md
15
SC2162.md
@@ -14,6 +14,13 @@ echo "Enter name:"
|
||||
read -r name
|
||||
```
|
||||
|
||||
or
|
||||
|
||||
```sh
|
||||
echo "Enter name:"
|
||||
IFS= read -r name
|
||||
```
|
||||
|
||||
### Rationale:
|
||||
|
||||
By default, `read` will interpret backslashes before spaces and line feeds (i.e. you can use backslashes in your string as an escape character). This is rarely expected or desired.
|
||||
@@ -24,7 +31,13 @@ Normally you just want to read data _including backslashes_ which are part of th
|
||||
>
|
||||
> If this option is given, backslash does not act as an escape character.
|
||||
|
||||
Note that [`read -r`](https://www.tldp.org/LDP/abs/html/internal.html#READR) will still strip leading and trailing spaces. `IFS="" read -r` prevents this.
|
||||
#### Trimming whitespace
|
||||
|
||||
Even with `read -r`, leading and trailing whitespace will be stripped from the input. Although this may sometimes be desirable or harmless it is often surprising and difficult to catch. Clearing the `IFS` disables this behavior, so `IFS= read -r` is generally safest.
|
||||
|
||||
### See Also
|
||||
|
||||
* [Bash FAQ 001](https://mywiki.wooledge.org/BashFAQ/001)
|
||||
|
||||
### Exceptions:
|
||||
|
||||
|
Reference in New Issue
Block a user