13
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

POSIX sh互換性メモ

Last updated at Posted at 2021-05-22

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

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

構文

☑︎ ! cmd1 | cmd2

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

2.9.2 Pipelines

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

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

Case Conditional Construct

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

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

Conditional Constructs (Bash Reference Manual)

* [[]]

[[]]形式の条件式。

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

Bash Conditional Expressions (Bash Reference Manual)

&>

Redirections (Bash Reference Manual)

コマンド

. での引数指定

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

dot - execute commands in the current environment

Bourne Shell Builtins (Bash Reference Manual)

source

Bash Builtins (Bash Reference Manual)

☑︎ cd -P

cd - change the working directory

export

☑︎ export var=val, export -p

export - set the export attribute for variables

-n

Bourne Shell Builtins (Bash Reference Manual)

read

☑︎ -r

read - read from standard input into shell variables

-p, -t, -n, -s

Bash Builtins (Bash Reference Manual)

☑︎ readonly var=val, readonly -p

readonly - set the readonly attribute for variables

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

return - return from a function or dot script

set

☑︎ -h, -o, -C

set - set or unset options and positional parameters

-k, -p, -t, -P

The Set Builtin (Bash Reference Manual)

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

umask - get or set the file mode creation mask

☑︎ unset -v, unset -f

unset - unset values and attributes of variables and functions

echo -n, echo -e

echo - write arguments to standard output

Bash Builtins (Bash Reference Manual)

test

☑︎ -e, -S

test - evaluate expression

-O, -G, -nt, -ot, -ef, ==, <, >

Bourne Shell Builtins (Bash Reference Manual)

☑︎ command

command - execute a simple command

* 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

☑︎ alias

alias - define or display aliases

☑︎ fc

fc - process the command history list

☑︎ unalias

unalias - remove alias definitions

パラメータ

☑︎ ${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

$_

Bash Variables (Bash Reference Manual)

☑︎ ${#var}

2.6.2 Parameter Expansion

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

2.6.2 Parameter Expansion

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

2.6.2 Parameter Expansion

${var:offset}, ${var:offset:len}

Shell Parameter Expansion (Bash Reference Manual)

${var/pat/sub}, ${var//pat/sub}

Shell Parameter Expansion (Bash Reference Manual)

置換・展開

☑︎ $()

2.6.3 Command Substitution

☑︎ $(())

2.6.4 Arithmetic Expansion

☑︎ ~

2.6.1 Tilde Expansion

参考文献

13
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?