Why Sponsor Oils? | source | all docs for version 0.23.0 | all versions | oilshell.org
Warning: Work in progress! Leave feedback on Zulip or Github if you'd like this doc to be updated.
POSIX shell has overlapping and quirky constructs for doing I/O:
echo
, printf
, and read
$(command sub)
constructmapfile
and readarray
YSH rationalizes I/O with:
write
builtinread
, like --all
$(string sub)
and @(array sub)
YSH also has orthogonal mechanisms for string processing:
${.myproc arg}
and @{.myproc arg}
are an optimization (TODO)${x %.2f}
as a static version of the printf
builtin (TODO)${x|html}
for safe escaping (TODO)These are discussed in more detail the strings doc.
echo
is flaky because echo $x
is a bug. $x
could be -n
.
write
accepts --
.read
is non-obvious because the -r
flag to ignore \
line continuations
isn't the default. The \
creates a mini-language that isn't understood by
other line-based tools like grep
and awk
.
$()
strips the trailing newline,.
read --all
, as well as lastpipe being on.Example:
hostname | read --all (&x)
write -- $x
write
: --qsn
, --sep
, --end
read
: --all
(future: --line
, --all-lines
?)$(string sub)
removes the trailing newline, if any@(array sub)
splits by IFS
IFS=$'\n'
?-sep
: Characters to separate each argument. (Default: newline)-end
: Characters to terminate the whole invocation. (Default: newline)-n
: A synonym for -end ''
.read
issue many read(0, 1)
calls. They do it
byte-by-byte.--long
flags to read
use buffered I/O.Here are some design notes on making the I/O builtins orthogonal and composable. There should be clean ways to "round trip" data between the OS and YSH data structures.
cat input.txt | read --all
# suppress the newline
write --end '' $_reply > output.txt
diff input.txt output.txt # should be equal
TODO
cat input.txt | read --all-lines :myarray
# suppress the newline
write --sep '' --end '' -- @myarray > output.txt
diff input.txt output.txt # should be equal
TODO