Top Nav

Archive | Bash Scripting

Bash Directory Stack

Ryan at Level1Techs introduces the Bash Directory Stack:

Quick Reference

The Bash Directory Stack is FILO (First In, Last Out)

pushd /some/dir/path

  • push pwd to stack and change to new directory


  • list stack

dirs -V

  • list stack vertical with index


  • change directory to directory at position 1 in stack

pushd +n

  • change directory to “n” indexed directory
  • actually a rotate
  • also accepts “-n”

popd +n

  • remove indexed item from stack
  • also accepts “-n”

dirs -c

  • clear stack

Get IP Address In A Script

Recently needed a good way to fetch the IP address of each interface within a script. Tried things like:


But this is fairly ugly. So I tried:

This gives a list of IP addresses but they are un-ordered so I can’t guarantee which address goes with which interface.  Here’s one using “ip addr”:

Still very messy.  Finally, found the “ifdata” command which is part of the “moreutils” package. First make sure “moreutils” is installed with:

Now you can query for a wide range of different information:

So here are some examples:

Overall this is much cleaner and more reliable then the earlier approaches.