Episode: 1843
Title: HPR1843: Some Bash tips
Source: https://hub.hackerpublicradio.org/ccdn.php?filename=/eps/hpr1843/hpr1843.mp3
Transcribed: 2025-10-18 10:00:21

---

This is HPR Episode 1843 entitled Some Bash Tips, and in part of the series, Bash Cripting.
It is hosted by Dave Morris, and in about 21 minutes long, the summer is a few useful
Bash features that may not be well known.
This episode of HPR is brought to you by AnanasThost.com.
Get 15% discount on all shared hosting with the offer code HPR15, that's HPR15.
Get your web hosting that's honest and fair at AnanasThost.com.
Hello everyone, this is Dave Morris. I've got a show for you today which I thought might
be of interest to some people, and it's about Bash. I've called it some Bash Tips, and
I'm going to talk about three Bash commands, which I'm not sure if they are as popular
as they used to be. You've heard me talk about this sort of thing before I'm a command line
user, and I have been using the Bash command line or the pre-decessors for quite some
time, and I wanted to talk about three commands related to directory movement, and they
are called push-d, pop-d, and does, D-I-R-S. So I've structured my full note, I've got
some long notes here, into a sort of basic overview section, in the assumption that if
you're interested in this, that might be enough for you, and then there's a more advanced
section, which goes into more of the guts of this stuff, so you're at liberty to skip
that if you want to, I warn you when I'm getting onto that bit, so you can fast forward if
that suits you. So let's start with push-d, push-d is normally followed by a directory name,
and it just changes directory like using CD, but it does a bit more than that. It saves
the previous directory, and the directory you've moved to in a stack. So I've given an
example of logging in, being in your home directory, I've typed PWD in my example to show
that I'm in my home directory, then I type push-d documents, and then I see a line that
starts with tilde slash documents, space tilde, and what that indicates is that that's
the stack, that's the representation of the stack, and the first element on the left
is the directory I'm currently in, that's the top of the stack, and the second one is
the directory I was in before, the tilde, that's my home directory. So it's showing it
in this way, which you need to get your head around a bit, but there are better ways
of showing it, which I'll come on to in a moment or two. So that's how you would change
directory, you can keep on pushing and pushing and pushing and moving on back in directories,
but when you want to go back to a directory, there's a complimentary command, pop-d, p-o-p-d,
this moves back to the previous directory. So in other words, it takes the topmost directory
off the stack, the one that you're currently in, takes it off the stack, and changes directory
to the one that's now the top, so effectively the second one working down the stack. In my
example, in the notes, I'm in the document directory, I type pop-d, and I get a line that
shows just a single tilde, and when I type p-w-d, I see I'm back in my home directory. You
can see what it's doing there. The stack can be any lengthy one, I think, I don't know
that there are any particular limits. There is another command which doesn't do any
movement, but it tells you about where you are in this stack, and things about the stack,
and it's doors, D-I-R-S, and it can display the directory's stack, and when you normally
type it, it shows it in that left to right format, so it shows the directory currently
in on the left, then the next one down the stack to the right of that, and the next one
to the right of that, and so forth. There's a nicer way of using it, or to my mind,
anyway, a nicer way of using it, which is to use an option, minus V, minus V to the doors
command shows the stack as a vertical list with index numbers. So, I've given an example,
where my example was before I was in the top level directory, so I pushed the documents
again, go back down to the documents directory, and there's another directory in there called
subder, just made for this demonstration, really, push D into that. So, what I get back
from the second push D is tilde slash documents slash subder, which is the directory I'm
in just now, followed by tilde slash documents, followed by tilde. If I then type doors minus
V, D, E, R, S, space, minus V, then I see a list that shows zero and tilde document slash
subder, one, tilde documents, two, tilde each on the separate line, and in a stack. So,
it shows it as the stack, and the top of the stack is the zero element, and it's the
one at the top of the list. So, the horizontal display is what's normally shown after
push D or a pop D, but you can force the vertical listing using the doors command. Okay, well,
that's the three commands in brief. Now, I'm going to dig down deeper and start talking about
them in more detail. They do have quite a lot more features than we've seen so far, and what I've
done is I've taken the man pages for these three commands, and I have reformatted them to make
to my mind a little bit more readable, and I've stuck them on the end of my full notes, which will
be available on the website naturally. The first point is you can use push D to add a directory to
the directory stack without changing to that directory. Use the minus A in option to do this.
So, my example shows we're back at the top directory again, the whole directory, and we type push D
space minus N space tilde slash documents. Didn't actually need the tilde slash, just documents
would have been enough because it would have been interpreted as the document's directory
immediately below where we currently are, but this is just me being pedantic. So, what we see in
this stack is tilde, which is the home directory, where we currently are. We haven't moved, followed by
tilde slash documents. What's happened is that the document's directory has been placed underneath
element zero on the stack, has been made into element one on the stack. So, the next example shows
push D, space minus N, space tilde slash documents slash subder. So, we're still in the top level
directory, so we're referring to the documents that directory with the sub directory underneath it,
and we add that to the stack, and that gets put at position one. So, when we see the horizontal
display of the stack, we see tilde, space, tilde slash documents slash subder, space tilde slash
documents. Now, type does minus V to show the thing vertically, and we see the same things numbered
zero one two. Now, the order of this stack is not the same as it was when we we visited these
directories by doing a push D down into each one, without the minus N. Now, this could be useful if
you wanted to preload the stack without actually visiting those directories, but you put them there,
because you wanted to visit them at a later stage. Another feature of push D is that you can
fiddle around with the order of the stack. Well, to be more precise, you can rotate it as if it's
a loop, so you can take the element one and push it to the top, and that causes element zero to go
right round to the bottom. So, it's a loop that rotates, and you can rotate it in either direction.
This is done using options that are written in the manual pages as plus N and minus N, but in this
case, N means an integer number, not the letter N. So, you'd use something like push D plus two,
that means rotate the stack so that entry number two counting from the left, or as you see it when
you're when you're number it with does minus V, it gets raised to the top, and then everything else
rotates appropriately, and I've shown it in an example, push D plus two rotates things so that
the element number two, in which we saw in the previous example, gets pushed at the top, so you get
tilde slash documents at the top, then after documents, if you looped round, would be tilde,
so you see that next, and after that is tilde slash documents slash subter. You can also use negative
numbers, that causes the rotation to have in the same direction, but it refers to the elements
starting from the bottom. You might find this confusing, I sure as hell do, and I've no
don't think I've ever used this, I've certainly done all this push D business of shuffling things round
in the in the past, that was the way I used to build up a stack of directories and then move
about between them, because if you do that, you don't ever lose them, they stay in the stack,
you just move them into different positions, but the negative one I've never found was all that
useful as probably shows my limitations of my brain, but still find it a bit weird. So my example,
I've used push D minus zero, what that says is minus zero means start counting at zero from the bottom
of the stack, and the way we left the stack, the previous example was with a document slash subter
at the bottom of the stack, element two if you're counting from from the top, so minus zero means
the bottom most one, it says bring it up to the top, but bringing it up to the top rotates the other
ones down appropriately, if you follow me, so it's when you do that, you see the directories listed
as, this is doing using the left to right notation, tilde slash document slash subter, that's the
one we just asked for to be on the top, then that's followed by tilde documents, which is the next one
in the loop of documents followed by tilde, you can't change the order, you can't swap tilde
documents until the document slash subter, you can't do that, you're just rotating this round
in a ring, I have to admit, I had never fully understood this, at least if I did, I'd forgotten it,
so in preparing for this show, I think I've got my head round it now, I've shown in this example
another option to the doors command, doors minus p, that simply lists the directories without
bothering to number them, given that the numbering can go either way depending on whether you're
using plus n or minus n in push d, push d, then it could be advantageous to do it that way, there's
no way of doing the numbering in the negative direction, actually it would be fun to write something
to provide that capability, I think about doing that one day, so that's pretty much all of
what push d can do, now let's look at pop d in a bit more detail, pop d of course again is documented
in my modified man page, it also takes a minus n where the actual letter n, which allows
to manipulate the stack without changing directory, so if we assume, I've refreshed everything,
I'm starting at the top level directory now, so first of all do a push d of documents, so my stack
consists of tilde slash documents followed by tilde, then if I add into that subter as I did
before, so I've got slash document slash subter on the top and then documents then tilde,
if I then do pop d minus n, what would that do, would you think?
Well the answer is it removes, removes, that is, we're not rotating now, it actually removes
from the directory stack the first element, element one, I'm not very good at this,
zero base counting stuff, it takes away tilde slash documents and you're left with
tilde slash document slash subter followed by tilde, so it doesn't actually change the
directory you're in, it doesn't change the top of the stack but it kills the next one down,
so I then show using pop d without any arguments, which we saw earlier on means
throw away the top of the stack and change directory to the next one down, which was
the tilde, so we're now at the home directory again, so I made a note here that pop d minus n
always takes away element one from the stack counting in the normal way, counting from left
to right, whereas plain pop d removes the zeroth element of the stack, we could also use the plus
and minus integer thing, so pop d plus three means to remove directory three from the stack counting
from the left and then and possibly confusingly pop d minus two means remove the the one counting
from the right to two positions from the right, this is not stack rotations I've pointed out in the
but explicit deletion, so I've got an example here which looks a bit daunting, but all I did was
create a top level directory called test one, when it with init another directory called test two
within that test three within that test four, so I just do a push d to each one of those in turn
and end up with a stack of zeroth element being tilde slash test one, test two, test three,
test four and so on down to the fourth element four I should say which is tilde, okay, I won't read
these out because you'll get so stunningly bored, so I probably, so if I then type pop d plus three
or do expect that to do, well that means take away the element which is numbered three in that
doors minus v listing that I included, so that was tilde slash test one, so do that and there you
go, tilde slash test one has been taken out of the directory stack, so then I did pop d plus zero,
that means take away the zeroth element which it does, but because you've now removed it and
that's normally the directory you're in, it changes directory for you to the one that's now become
the top of the stack which is test one slash test two slash test three, then finally in this
this example I use the negative integer business pop d minus one takes the element which is one
position counting from zero from the right, so in the stack we've got test one, test two, test three
followed by test one, test two followed by a home directory tilde, so counting minus one
skips over the tilde and goes to test one, test two, so we remove that and all we're left with is
test one, test two, test three and then tilde, shouldn't it call these test ones and things,
it would have turned a better, if that just called them one, two, three wouldn't it, oh well,
seem like a good idea at the time, anyway I do a doors minus v at the end to show you what's left,
just to prove the point, finally let's look at the doors command in a bit more detail and again
I've put a version of the man page in the notes and you've seen me using doors minus v to show the
directory stack in this what I think to be a more readable form with numbers and you've also
seen me using the minus p option where it does the same but without numbers, there's also a minus
l option which lists the directories but gives the full path name so it replaces the tilde
with the explicit home directory, haven't demonstrated that, you can with the doors command just
clear the entire directory stack using minus c, it just drops the whole thing
and but it leaves you in the directory you were before you issued the command, it doesn't,
doesn't move you around within the stack in any any sense it just blitzes it, now you can use
the doors command to make it clear to yourself which directory you will get if you use one of
this plus or minus integer options and I've done an example here to demonstrate this, what I've
done is I've gone back to the top level directory then I've gone all the way through this test one,
two, three business again, I've just done the wall on the same line pushing in the
wall onto the stack so we end up with a stack of all these directories as before but I won't read
out then I typed doors plus three meaning tell me which directory is number three on the stack in
other words it's it's number three counting from zero from the left and the answer is tilde slash
test one and I do a doors minus V so you can improve that that's that's the case so if you
were going to type a pop D or a push D or something with a plus three in it it might be useful to
use that first just to check that it's going to do what you want it to do then I also do do does
minus three just to prove that counting up from the bottom or from the the right if you prefer to
think of it in that term gets you to where you want to be so minus three there's three places
it knows the three places counting from zero so it shows the directory test one test two test three
it's actually hard to say this hope you've got the gist of what I'm trying to to explain you
so I hope I haven't driven you away from this rather than attracting you towards it by all of this
whiter but I think it can be quite useful now written a short conclusion here why use them what
use are they well I actually started using these long time ago pre linux when I was working on
older unique systems like Dexaltrix and Sun Sunos and Solaris and HPUX HPUX operating system
all of which were versions of Unix that had SH or Bash shells on them and they they did have
pop pushed in popdy and some at some point it was real terminals I would been using with these
things and so you just had the one you don't have windows at all you just had a terminal so you
were you were there was a non graphical display so there was there was times when being
able to jump around between um directories in this way was quite fun and it was before the days
of terminal emulators and windowed systems and virtual test stops and all these other goodies so
I used to be able to since my job entailed often dealing with people's problems as well as doing
some development or or system management or something it was often necessary to stop what I was doing
and then go and do some something somewhere else in a directory on the system and then come back
to what I was doing so it was really useful to be able to use pushd to go to that other place
do the thing find the answer or whatever it was and then say right that's dealt with now I can go
back to what I was doing and do popdy back to where I'd come from it's I guess it's less of an
issue these days I personally tend to have several terminal emulators on several virtual
desktops on my workstation and in each terminal emulator I've usually got multiple tabs
and I I'm in a different directory on each one and I hop around between tabs and screens and
this sort of thing so I tend to use pushed in popdy less but there's on there's occasions where
I am wanting to to step away from what I'm doing just to do something else briefly and then come
back to it without opening another tab or something and I find it useful to have that in my toolkit
so you might find it useful as well hope you do anyway okay that's it bye
you've been listening to Heka Public Radio at Heka Public Radio dot org
we are a community podcast network that releases shows every weekday Monday through Friday
today's show like all our shows was contributed by an hbr listener like yourself if you ever
thought of recording a podcast then click on our contributing to find out how easy it really is
Heka Public Radio was founded by the digital dog pound and the infonomican computer club
and it's part of the binary revolution at binrev.com if you have comments on today's show
please email the host directly leave a comment on the website or record a follow up episode yourself
unless otherwise stated today's show is released on the creative comments attribution share
freedom license
