Bash – a *.sorted command and why doesn’t it work in the bash

awkbashdiff()environment-variables

In a Bash Guide for Beginners (sec. 3.2.1.2. Local variables) there is an example command which is supposed to list all of the local variables, that are not in environmental variables, or so I think:

diff set.sorted printenv.sorted | grep "<" | awk '{ print $2 }'

I believe I understand everything about this command except the *.sorted part. Furthermore, this command doesn't work on my Bash, specifically because of the above elements. Here is the response (it's the Polish version of No such file or directory):

diff: set.sorted: Nie ma takiego pliku ani katalogu
diff: printenv.sorted: Nie ma takiego pliku ani katalogu

Worth noting is that I use Ubuntu 14.04 with close to no changes to its default installation.

Because of this, I am unable to analyze this command in my Bash shell, nor was I able to find anything about this on Google except for the very tutorial I took this command from.

I will be grateful for a comprehensive explanation since I never encountered such an expression before.

Best Answer

They're not expressions, they're filenames for files produced as follows:

 printenv | sort > printenv.sorted
 set | sort > set.sorted

That's not clear from the documentation so your confusion is understandable!

Note that you may need to help diff and grep by forcing them to treat their inputs as text (with -a); environment variables can contain values which will cause them to treat their input as binary, which won't produce anything useful:

diff -a set.sorted printenv.sorted | grep -a "<" | awk '{ print $2 }'

Better still, use comm to compare the two files:

comm -23 set.sorted printenv.sorted

You can replace the files with process substitutions:

comm -23 <(set|sort) <(printenv|sort)

to avoid creating files.