Introduction
Announcements

Schedule
Labs
Assignments
TA office hours

Tests, exam

Topic videos
Some course notes
Extra problems
Lecture recordings

Discussion board

Grades so far

1. [15 marks] For each of the following pairs of sh commands, is the behaviour the same or different? If the behaviour is the same, explain why the difference in the command has no effect on the behaviour. If the behaviour is different, state the behaviour of each of the two commands.

a)

	echo hello, world
as opposed to
	echo 'hello, world'

Answer: The behaviour is the same. In the first case, the echo command sees two command-line arguments, "hello," and "world"; in the second case, the echo command sees just one command-line argument, "hello, world". But in the event of multiple arguments, echo outputs them all separated by spaces, so the output is identical.

b)

	x=hello
	echo '$x, world'
as opposed to
	x=hello
	echo "$x, world"

Answer: The first outputs "$x, world" and the second outputs "hello, world", because single quotes suppress the special meaning of the dollar sign whereas double-quotes do not.

c)

	cat file
as opposed to
	cat <file

Answer: The output is the same, because cat will read its standard input if and only if there are no command-line file names.

d)

	sort file | tr x y
as opposed to
	sort file | tr x y | sort

Answer: The output may be different because the 'tr' may change the sort order.

e)

	cat file >file2
as opposed to
	cat file >file2 2>&1

Answer: If the file exists and is read without error, the behaviour is the same because "2>&1" redirects stderr and there is no stderr output. However, if there is an error of some kind, in the first case the error message is displayed to the stderr of the invoking environment, whereas in the second case the error message goes into "file2".


[press 'back' in your web browser to return to where you were in the exam]