POSIX sh互換性メモ


[『［改訂第3版］シェルスクリプト基本リファレンス ──#!/bin/shで、ここまでできる』](https://gihyo.jp/book/2017/978-4-7741-8694-8)[1]はFreeBSD, Solaris, BusyBoxのshとbashでの各種機能の対応状況を説明しているけれど、ところでPOSIX的にはどうなんだっけというのが気になったので調べてみた。

[1]でFreeBSD, Solaris, BusyBoxのshでサポート状況が食い違う機能について、POSIXに含まれるものを☑︎、POSIXで陽にunspecifiedとされているものを*、POSIXに含まれないものを☐とし、可能であればそれぞれPOSIX、bashまたは該当シェルのマニュアルへのリンクを掲げた。

# 構文
## ☑︎ `! cmd1 | cmd2`

`!`でパイプライン全体の結果を否定できるかどうか。

[2.9.2 Pipelines](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_02)

## ☑︎ `case arg in (pat) ...;; esac`

`case`のパターンの前に`(`を書けるかどうか。

[Case Conditional Construct](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_04_05)

## ☐ `case` の `;&`

bashやFreeBSDのshでは`case`内で`;;`の代わりに`;&`とすることでfallthroughできる。

> If the selected list is terminated by the control operator \`;&' instead	of \`;;', execution continues with the next list, continuing until a list terminated with \`;;' or the end of the case command.

[SH(1) FreeBSD	General	Commands Manual](https://www.freebsd.org/cgi/man.cgi?query=sh&sektion=&n=1)

> Using ‘;&’ in place of ‘;;’ causes execution to continue with the command-list associated with the next clause, if any.

[Conditional Constructs (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Conditional-Constructs.html#index-case)


## * `[[` 〜 `]]`

`[[` 〜 `]]`形式の条件式。

> The following words may be recognized as reserved words on some implementations (when none of the characters are quoted), causing unspecified results:

[2.4 Reserved Words](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_04)

[Bash Conditional Expressions (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bash-Conditional-Expressions.html#Bash-Conditional-Expressions)

## ☐ `&>`

[Redirections (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Redirections.html#Redirecting-Standard-Output-and-Standard-Error)

# コマンド
## ☐ `.` での引数指定

`.` に追加の引数を指定することで、読まれる側のファイルの位置パラメータを設定できるかどうか。

[dot - execute commands in the current environment](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#dot)

[Bourne Shell Builtins (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-_002e)

## ☐ `source`

[Bash Builtins (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-source)

## ☑︎ `cd -P`

[cd - change the working directory](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/cd.html)

## `export`

### ☑︎ `export var=val`, `export -p`

[export - set the export attribute for variables](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#export)

### ☐ `-n`

[Bourne Shell Builtins (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-export)

## `read`

### ☑︎ `-r`

[read - read from standard input into shell variables](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html)

### ☐ `-p`, `-t`, `-n`, `-s`

[Bash Builtins (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-read)

## ☑︎ `readonly var=val`, `readonly -p`

[readonly - set the readonly attribute for variables](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#readonly)

## ☑︎ `.` されたファイル内での `return`

[return - return from a function or dot script](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#return)

## `set`

### ☑︎ `-h`, `-o`, `-C`

[set - set or unset options and positional parameters](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set)

### ☐ `-k`, `-p`, `-t`, `-P`

[The Set Builtin (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/The-Set-Builtin.html#index-set)

## ☑︎ `umask -S`, `chmod`形式でのumask指定

[umask - get or set the file mode creation mask](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/umask.html)

## ☑︎ `unset -v`, `unset -f`

[unset - unset values and attributes of variables and functions](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset)

## ☐ `echo -n`, `echo -e`

[echo - write arguments to standard output](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/echo.html)

[Bash Builtins (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html#index-echo)

## `test`

### ☑︎ `-e`, `-S`

[test - evaluate expression](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html)

### ☐ `-O`, `-G`, `-nt`, `-ot`, `-ef`, `==`, `<`, `>`

[Bourne Shell Builtins (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bourne-Shell-Builtins.html#index-test)

## ☑︎ `command`

[command - execute a simple command](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/command.html)

## * `builtin`, `let`, `local`, `bind`, `history`, `suspend`

> If the command name matches the name of a utility listed in the following table, the results are unspecified.

[Command Search and Execution](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01_01)


## ☑︎ `alias`

[alias - define or display aliases](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/alias.html)

## ☑︎ `fc`

[fc - process the command history list](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/fc.html)

## ☑︎ `unalias`

[unalias - remove alias definitions](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/unalias.html)

# パラメータ
## ☑︎ `${10}` etc.

> When a positional parameter with more than one digit is specified, the application shall enclose the digits in braces (see Parameter Expansion). 

[2.5.1 Positional Parameters](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_01)

## ☐ `$_`

[Bash Variables (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html#index-_0024_005f)

## ☑︎ `${#var}`

[2.6.2 Parameter Expansion](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02)

## ☑︎ `${var#pat}`, `${var##pat}`

[2.6.2 Parameter Expansion](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02)

## ☑︎ `${var%pat}`, `${var%%pat}`

[2.6.2 Parameter Expansion](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02)

## ☐ `${var:offset}`, `${var:offset:len}`

[Shell Parameter Expansion (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion)

## ☐ `${var/pat/sub}`, `${var//pat/sub}`

[Shell Parameter Expansion (Bash Reference Manual)](https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html#Shell-Parameter-Expansion)

# 置換・展開
## ☑︎ `$(` 〜 `)`

[2.6.3 Command Substitution](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_03)

## ☑︎ `$((` 〜 `))`

[2.6.4 Arithmetic Expansion](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_04)

## ☑︎ `~`

[2.6.1 Tilde Expansion](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_01)

# 参考文献

* [1] [『［改訂第3版］シェルスクリプト基本リファレンス ──#!/bin/shで、ここまでできる』](https://gihyo.jp/book/2017/978-4-7741-8694-8)（山森丈範、技術評論社、2017）
* [2] [POSIX.1-2017](https://pubs.opengroup.org/onlinepubs/9699919799/)
* [3] [Bash Reference Manual](https://www.gnu.org/software/bash/manual/html_node/index.html)
* [4] [SH(1) FreeBSD	General	Commands Manual](https://www.freebsd.org/cgi/man.cgi?query=sh&sektion=&n=1)