POSIX #!/bin/sh

By Hal Canary, 2012-06-19 06:30:49 (link)
#computers-code

When writing shell scripts to be executed on Linux, Unix, or Mac OS, one can target bash or tcsh. I always reccomend targeting the standard Bourne Shell, since it is guaranteed to exist on POSIX-compatable systems. Unforntunately, a lot of bash-specific documentation exists. So we can go to the source for shell standard: the Single UNIX Specification, The Standard Command Language Interpreter and The Shell Command Language.

Update: here's a list of all the shell utilities POSIX requires to exist:
admin alias ar asa at awk basename batch bc bg break c99 cal cat cd cflow chgrp chmod chown cksum cmp : comm command compress continue cp crontab csplit ctags cut cxref date dd delta df diff dirname dot du echo ed env eval ex exec exit expand export expr false fc fg file find fold fort77 fuser gencat get getconf getopts grep hash head iconv id ipcrm ipcs jobs join kill lex link ln locale localedef logger logname lp ls m4 mailx make man mesg mkdir mkfifo more mv newgrp nice nl nm nohup od paste patch pathchk pax pr printf prs ps pwd qalter qdel qhold qmove qmsg qrerun qrls qselect qsig qstat qsub read readonly renice return rm rmdel rmdir sact sccs sed set sh shift sleep sort split strings strip stty tabs tail talk tee test time times touch tput tr trap true tsort tty type ulimit umask unalias uname uncompress unexpand unget uniq unlink unset uucp uudecode uuencode uustat uux val vi wait wc what who write xargs yacc zcat

Of these utilities, on my Ubuntu 12.04 system:

Shell builtin or keyword (bash):
: alias bg break cd command continue echo eval exec exit export false fc fg getopts hash jobs kill printf pwd read readonly return set shift test time times trap true type ulimit umask unalias unset wait

Located in /bin or /usr/bin
ar at awk basename batch bc c99 cal cat chgrp chmod chown cksum cmp comm cp crontab csplit ctags cut date dd df diff dirname dot du ed env ex expand expr file find fold fuser gencat getconf head iconv id ipcrm ipcs join link ln locale localedef logger logname lp m4 make man mesg mkdir mkfifo more mv newgrp nice nl nm nohup od paste patch pathchk pr ps renice rm rmdir sed sh sleep sort split strings strip stty tabs tail tee touch tput tr tsort tty uname uncompress unexpand uniq unlink vi wc who write xargs zcat

Not found:
admin asa cflow compress cxref delta fort77 get lex mailx pax prs qalter qdel qhold qmove qmsg qrerun qrls qselect qsig qstat qsub rmdel sact sccs talk unget uucp uudecode uuencode uustat uux val what yacc

dash (/bin/sh) has a smaller set of builtins: it lacks fc and time. /usr/bin/time exists, but there is no fc in dash.


(back)