In some Bourne-like shells, the read
builtin can not read the whole line from file in /proc
(the command below should be run in zsh
, replace $=shell
with $shell
with other shells):
$ for shell in bash dash ksh mksh yash zsh schily-sh heirloom-sh "busybox sh"; do
printf '[%s]\n' "$shell"
$=shell -c 'IFS= read x </proc/sys/fs/file-max; echo "$x"'
done
[bash]
602160
[dash]
6
[ksh]
602160
[mksh]
6
[yash]
6
[zsh]
6
[schily-sh]
602160
[heirloom-sh]
602160
[busybox sh]
6
read
standard requires the standard input need to be a text file, does that requirement cause the varied behaviors?
Read the POSIX definition of text file, I do some verification:
$ od -t a </proc/sys/fs/file-max
0000000 6 0 2 1 6 0 nl
0000007
$ find /proc/sys/fs -type f -name 'file-max'
/proc/sys/fs/file-max
There's no NUL
character in content of /proc/sys/fs/file-max
, and also find
reported it as a regular file (Is this a bug in find
?).
I guess the shell did something under the hood, like file
:
$ file /proc/sys/fs/file-max
/proc/sys/fs/file-max: empty