Mirrored from GitHub

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

Jump to: README.md posix.txt


README.md

1	Credit 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	
3	These 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	
7	POSIX (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	
9	It is found in /bin/sh, although it may be symlinked to bash or you can symlink to dash for performance

posix.txt

1	Command substitution
2	
3	Back 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	
7	The current directory is `pwd`
8	
9		$ echo 'The current directory is \`pwd\`'
10	
11	The current directory is \`pwd\`
12	
13		$ echo "The current directory is `pwd`"
14	
15	The current directory is /home/avsbq
16	
17		$ echo "The current directory is \`pwd\`"
18	
19	The current directory is `pwd`
20	
21	This 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	
25	Using the above, the previous examples would be:
26	
27		$ echo 'The current directory is $(pwd)'
28	
29	The current directory is $(pwd)
30	
31		$ echo "The current directory is $(pwd)"
32	
33	The current directory is /home/avsbq
34	
35		$ echo "The current directory is \$(pwd)"
36	
37	The current directory is $(pwd)
38	
39	An 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	
47	Alternatively we can use nest expressions:
48	 
49		echo "next year is $(expr $(date +%Y) + 1)"