Mirrored from GitHub

git clone https://github.com/christc4/posix-shell-notes.git

Jump to: README.md posix.txt


README.md

1Credit goes to: [grymoire.com](https://www.grymoire.com/Unix/Sh.html#uh-3), [drewdevault.com](https://drewdevault.com/2018/02/05/Introduction-to-POSIX-shell.html), [standard](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html)
2
3These are my personal reference notes, I do not claim the examples or definitions below as originally authored by myself
4
5## What is POSIX?
6
7POSIX (Portable Operating System Interface) is the standard UNIX shell - standard as in formally defined and shipped in a published standard, making shell scripts written for it portable. POSIX, is essentially a formalized version of Bourne.
8
9It is found in /bin/sh, although it may be symlinked to bash or you can symlink to dash for performance

posix.txt

1Command substitution
2
3Back quotes aren't used to prevent interpretation of special characters it *instead* has a special use - _command substitution_. The string between backquotes is executed by the shell, and the results *replace* the backquoted string:
4
5	$ echo 'The current directory is `pwd`'
6
7The current directory is `pwd`
8
9	$ echo 'The current directory is \`pwd\`'
10
11The current directory is \`pwd\`
12
13	$ echo "The current directory is `pwd`"
14
15The current directory is /home/avsbq
16
17	$ echo "The current directory is \`pwd\`"
18
19The current directory is `pwd`
20
21This feature hail from earlier shells (C and Bourne). This has a major problem - command substitution cannot be nested. A new mechanism was created for command substitution, which replaces the back quotes:
22
23`$(command)`
24
25Using the above, the previous examples would be:
26
27	$ echo 'The current directory is $(pwd)'
28
29The current directory is $(pwd)
30
31	$ echo "The current directory is $(pwd)"
32
33The current directory is /home/avsbq
34
35	$ echo "The current directory is \$(pwd)"
36
37The current directory is $(pwd)
38
39An example command substitution of adding one to the current year.
40
41	YEAR="$(date +%Y)"
42
43	YEAR="$(expr $YEAR + 1)"
44
45	echo "next year is $YEAR"
46
47Alternatively we can use nest expressions:
48 
49	echo "next year is $(expr $(date +%Y) + 1)"