This page though short, will imbue you with subtle wiles from which you may derive great Profit. None typeth long upon the Line of Command without soon making the acquaintance of the simple command ‘cd’, ‘see dee’. Some say the name means to change directory, yet verily the name originates from the Latin phrase ‘carpe diem’. If agreement cannot be reached betwixt these divers opinions, let it be known as ‘carpe directory’ in compromise.
And so the implement ‘cd’ is surpassing simple, thus ye may wonder what tricks it may it have for us. Two we will speak of, one direct and one rather more indirect.
The first is a special option to ‘cd’, which is so bare and simple it may seem a mistake at first glance. It is like an Option yet with no further character. A bare dash, alone, nothing before and nothing after, ‘cd -’, see dee dash. And what is wrought by this? It acts like a Toggle, switching thy working directory back to the previous, and then back again, back and forth with each issuance. Id est,
Oft this is passing useful, as we switch back and forth, to list directory contentes, or to copy Files hither and yon. And so we come to the indirectly related aspect from ‘cd’, which is that it sets special Variables within thy Environment. One is ‘$PWD’, which is the Present Working Directory. This is of some interest and occasional use, but the true value is in its compatriot, ‘$OLDPWD’. For this is as you may guess, the Other Last Directory Previously on Working Duty, or mayhap as some claim, simply the Old PWD. Regardless, it can be exceeding useful. Id est,
As shewn, the setting of ‘$OLDPWD’ is not reliant upon using ‘cd -’. It is set whenever thy directories changeth by the use of our stalwart Friend ‘cd’.
Ctrl-D, Amen.