5 ms·
"Quoted string notation" (https://www.oilshell.org/release/latest/doc/qsn.html https://www.oilshell.org/release/latest/doc/qsn.html) seems like a good way to so
by NumberWangMan 5y ago
"Quoted string notation" (https://www.oilshell.org/release/latest/doc/qsn.html https://www.oilshell.org/release/latest/doc/qsn.html) seems like a good way to solve this problem.
- chubot 5y ago(author here) Yes thanks, that is exactly the point! As I point out at the end of the doc, coreutils ls actually started quoting the names in 2016. However the format is confusing for people who can't read 2 or 3 types of shell strings, and not that readable. In contrast, QSN is simply Rust string literal syntax, which are a cleaned up version of C string literal syntax. $ touch $'foo\nbar' 'dq"dq' "sq'sq" # create 3 files with newline, double quote, single quote # coreutils is correct, though I'm not sure people will understand $'\n' $ ls 'dq"dq' eggs 'foo'$'\n''bar' "sq'sq" Pipe through cat mangles the name: $ ls|cat dq"dq foo bar sq'sq In Oil, write --qsn will ALWAYS give you 5 lines if you have 5 names, no matter what they are $ oil -c 'write --qsn -- *' 'dq"dq' 'foo\nbar' # more familiar encoding 'sq\'sq' Without --qsn it's like ls|cat: $ oil -c 'write -- *' dq"dq foo bar sq'sq I think it's important for something like QSN to be built into the shell, because quoting issues arise in many places, not just filenames and ls. Although this makes me think that we should have the inverse of `printf %q` to parse the output of coreutils ls. Oil does implement printf %q, but most people don't know about it. $ printf '%q\n' -- * dq\"dq $'foo\nbar' sq\'sq Again it is actually correct, but sort of a grab bag of formats derived from shell strings. QSN strings will be familiar to anyone using Python, Rust, etc. consistent with Oil's slogan: It's for Python and JavaScript users who avoid shell! ----- edit: Also reminds me that I wrote this page before designing QSN: https://github.com/oilshell/oil/wiki/Shell-Almost-Has-a-JSON-Analogue https://github.com/oilshell/oil/wiki/Shell-Almost-Has-a-JSON... So printf %q and %b are inverses in bash, but this doesn't work in other shells. QSN can represent NUL bytes, which are illegal in filenames, but are useful elsewhere.