summaryrefslogtreecommitdiffstats
path: root/guides/shell-workshop/shell.txt
blob: d98d2493c1b9c2c66097d22516db6f0080ce242e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
introduce the shell interpreter
	POSIX.1-2008 XCU 2.1
	shell reads input from:
		a specified file,
		'-c' option argument, or
		stdin (interactive mode)
	shell splits input lines into tokens and parses them
	shell expands words
	shell performs I/O redirection
	shell executes functions, built-in commands, files, etc.
parameters and variables
	positional parameters
		POSIX.1-2008 XCU 2.5.1
		decimal numbers greater than 0, e.g. $1 and $2
		multiple-digit numbers should be enclosed in braces
		assigned with:
			shell arguments when shell starts
			function arguments when a function is called
		can be reassigned using 'set' built-in command
	special parameters
		POSIX.1-2008 XCU 2.5.2
		@
			all positional parameters
		*
			all positional parameters
		#
			number of positional parameters
		?
			exit status of most recent command pipeline
		-
			current option flags
		$
			process ID of shell
		!
			process ID of most recent background command
		0
			name of shell or shell script
	shell variables
		POSIX.1-2008 XCU 2.5.3
		HOME
			pathname of user's home directory
		IFS
			delimiters for field splitting
			default: <space> <tab> <newline>
		LINENO
			line number of current script, if any
		PATH
			list of paths which should be searched for commands
		PS1
			prompt value to be expanded and written to standard error
			default value: "$ "
		PWD
			absolute pathname of the current working directory
word expansions
	tilde expansion
		POSIX.1-2008 XCU 2.6.1
		tilde-prefix: unquoted tilde at the beginning of a word
			followed by all unquoted characters until first unquoted slash
		login name is all characters in tilde-prefix following tilde
		if login name is empty (tilde-prefix == "~")
			expanded to value of HOME
		else
			expanded to home directory associated with login name
	parameter expansion
		POSIX.1-2008 XCU 2.6.2
		${expression} or $expression
		expression:
			parameter
			parameter:-word
			parameter:=word
			parameter:?word or parameter:?
			parameter:+word
			#parameter
			parameter%word
			parameter%%word
			parameter#word
			parameter##word
	command substitution
		POSIX.1-2008 XCU 2.6.3
		$(command)
		or
		`command`
		expanding:
			shell executes command in a subshell
			shell replaces substitution with stdout of the command
			newlines within command are allowed
		nesting
			`command1 \`command2 foo\` bar`
			$(command1 $(command2 foo) bar)
	arithmetic expansion
		POSIX.1-2008 XCU 2.6.4
		$((expression))
		expanding:
			shell processes expression
			shell replaces substitution with value of the expression
		arithmetic:
			signed long integer precision
			decimal, octal, and hexadecimal constants
				e.g. 42, 052, 0x2A
			operators:
				( ), unary +, unary -, +, -, *, /, %
				~, !, <<, >>, &, ^, |
				<, <=, >, >= ==, !=, &&, ||, expr ? expr : expr
				=, +=, -=, *=, /=, %=, <<=, >>=, &= ^=, |=
			shell variables
				if x has an integer value, then the following are equivalent:
				$((x + 1))  $(($x + 1))
	field splitting
		POSIX.1-2008 XCU 2.6.5
		IFS is a set of delimiters, at which unquoted fields are split
	pathname expansion
		POSIX.1-2008 XCU 2.6.6
		POSIX.1-2008 XCU 2.13
		performed unless 'set -f' was run
		patterns
			? matches any single character
			* matches multiple (0 or more) characters
			[ opens RE bracket expansion
		if pattern does not match any existing files
			pattern string is left unchanged
redirection
	input redirection
		[n]<word
		n: optional file descriptor number (default: 0, stdin)
		word: name of file
	output redirection
		[n]>word
		[n]>|word
		n default: 1, stdout
	appending redirected output
		[n]>>word