Known Differences Between OSH and Other Shells

This document is for sophisticated shell users.

You're unlikely to encounter these incompatibilities in everyday shell usage. If you do, there's almost always a simple workaround, like adding a space or a backslash.

OSH is meant to run all POSIX shell programs, and most bash programs.

Table of Contents
Numbers and Arithmetic
Static Parsing
No Coercion to Zero
printf '%d' and other numeric formats require a valid integer
Parsing Differences
Strings vs. Bare words in array indices
Subshell in command sub
Extended glob vs. Negation of boolean expression
Here doc terminators must be on their own line
break / continue / return are keywords, not builtins
Spaces aren't allowed in LHS indices
More Parsing Differences
Assignments can't have redirects
Variable names in assignments must be constants
Brace expansion is all or nothing
Tilde expansion and Brace expansion don't interact
Brackets should be escaped within character classes
Double quotes within backticks
Differences at Runtime
Alias Expansion
Arrays aren't split inside ${}
Touching errexit while it's temporarily disabled
Completion
Interactive Features
History Substitution Language
Links

Numbers and Arithmetic

Roughly speaking, shells treat arithmetic like "macro processing", while OSH treats it more like part of a programming language.

Despite these differences, OSH is very compatible with existing shell scripts.

Static Parsing

Arithmetic is statically parsed, so expressions like $(( 1 $op 2 )) fail with a parse error. Use an explicit eval for these rare use cases.

No Coercion to Zero

Strings like '' or 'foo' aren't coerced to 0 in arithmetic contexts. OSH produces a fatal error unless you opt out with shopt -u strict-arith. (This is the only strict option that's on by default.)

printf '%d' and other numeric formats require a valid integer

In other shells, printf %d invalid_integer prints 0 and a warning. OSH gives you a runtime error.

Parsing Differences

This section describes differences related to static parsing. OSH avoids the dynamic parsing of most shells.

(Note: This section should encompass all the failures from the wild tests and spec tests.

Strings vs. Bare words in array indices

Strings should be quoted inside array indices:

No:

"${SETUP_STATE[$err.cmd]}"

Yes:

"${SETUP_STATE["$err.cmd"]}"

When unquoted, the period causes an ambiguity with respect to regular arrays vs. associative arrays. See Parsing Bash is Undecidable.

Subshell in command sub

You can have a subshell in a command sub, but it usually doesn't make sense.

In OSH you need a space after $(. The characters $(( always start an arith sub.

No:

$((cd / && ls))

Yes:

$( (cd / && ls) )   # Valid but usually doesn't make sense.
$({ cd / && ls; })  # Use {} for grouping, not {}.  Note trailing ;
$(cd / && ls)       # Even better

Extended glob vs. Negation of boolean expression

In bash the rules are more complicated, and depend on shopt -s extglob. The extglob setting does nothing in OSH.

Here doc terminators must be on their own line

Lines like EOF] or EOF) don't end here docs. The delimiter must be on its own line.

No:

a=$(cat <<EOF
abc
EOF)

a=$(cat <<EOF
abc
EOF  # this is not a comment; it makes the EOF delimiter invalid
)

Yes:

a=$(cat <<EOF
abc
EOF
)  # this is actually a comment

break / continue / return are keywords, not builtins

This means that they aren't "dynamic":

b=break
while true; do
  $b  # doesn't break in OSH
done

Static control flow will allow static analysis of shell scripts.

(Test cases are in spec/loop).

Spaces aren't allowed in LHS indices

Bash allows:

a[1 + 2 * 3]=value

OSH only allows:

a[1+2*3]=value

because it parses with limited lookahead. The first line would result in the execution of a command named a[1.

More Parsing Differences

These differences occur in subsequent stages of parsing, or in runtime parsing.

Assignments can't have redirects

No:

x=abc >out.txt
x=${y} >out.txt
x=$((1 + 2)) >out.txt

# This is the only one that makes sense, but is still disallowed.
x=$(echo hi) >out.txt

Yes:

x=$(echo hi >out.txt)

The first three constructs don't make sense, and the fourth has a clearer alternative spelling, so OSH disallows such redirects altogether.

Variable names in assignments must be constants

That is, they can't be variables themselves.

No:

declare "$1"=abc

Yes:

declare x=abc

NOTE: This restriction will probably be relaxed.

Brace expansion is all or nothing

No:

{a,b}{        # what does the second { mean?
{a,b}{1...3}  # 3 dots instead of 2

Yes:

{a,b}\{
{a,b}\{1...3\}

bash will do a partial expansion in the former cases, giving you a{ b{ and a{1...3} b{1...3}.

OSH considers them syntax errors and aborts all brace expansion, giving you the same thing back: {a,b}{ and {a,b}{1...3}.

Tilde expansion and Brace expansion don't interact

In bash, {~bob,~jane}/src will expand the home dirs of both people. OSH doesn't do this because it separates parsing and evaluation. By the time tilde expansion happens, we haven't evaluated the brace expansion. We've only parsed it.

(mksh agrees with OSH, but zsh agrees with bash.)

Brackets should be escaped within character classes

Don't use ambiguous syntax for a character class consisting of a single bracket character.

No:

echo [[]
echo []]

Yes:

echo [\[]
echo [\]]

The ambiguous syntax is allowed when we pass globs through to libc, but it's good practice to be explicit.

Double quotes within backticks

In rare cases, OSH processes backslashes within backticks differently than other shells. However there are two workarounds that are compatible with every shell.

No:

`echo \"`     # is this a literal quote, or does it start a string?

Yes:

$(echo \")    # $() can always be used instead of ``.
              # There's no downside to the more modern construct.
`echo \\"`    # also valid, but $() is more readable

Notes:

Differences at Runtime

Alias Expansion

Almost all "real" aliases should work in OSH. But these don't work:

alias left='{'
left echo hi; }

(cases #33-#34 in spec/alias)

or

alias a=
a (( var = 0 ))

Details on the OSH parsing model:

  1. Your code is statically parsed into an abstract syntax tree, which contains many types of nodes.
  2. SimpleCommand are the only ones that are further alias-expanded.

For example, these result in SimpleCommand nodes:

These don't:

Arrays aren't split inside ${}

Most shells split the entries of arrays like "$@" and "${a[@]}" here:

echo ${undef:-"$@"}

In OSH, omit the quotes if you want splitting:

echo ${undef:-$@}

I think OSH is more consistent, but it disagrees with other shells.

Touching errexit while it's temporarily disabled

In all shells, checks for non-zero status are disabled in these situations:

  1. The condition of the if, while, and until constructs
  2. A command/pipeline prefixed by !
  3. Every clause in || and && except the last.

Now consider this situation:

This is a fatal error in OSH. Other shells delay the restoration of errexit until after the temporary disablement.

Good articles on errexit:

OSH also has strict-errexit to fix two issues with bash's behavior:

Completion

The OSH completion API is mostly compatible with the bash completion API, except that it moves the responsibility for quoting out of plugins and onto the shell itself. Plugins should return candidates as argv entries, not shell words.

See the OSH manual for details.

Interactive Features

History Substitution Language

The rules for history substitution like !echo are simpler. There are no special cases to avoid clashes with ${!indirect} and so forth.

TODO: Link to the history lexer.

Links

External:


Generated on Mon Jul 1 11:41:45 PDT 2019