Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Bookmark directories in #Bash.

Bookmark directories in Bash.


Source in your ~/.bashrc. (Enableing cdable_vars in Bash makes this script better.)


Bookmark current directories:

$ bmark name

Recommended name format: [-A-Za-z0-9_]+. Avoid special characters.

Use basename as the default bookmark name:

$ bmark

List all bookmarks:

$ bmarks

Go to one bookmark:

$ cd $name

If you've shopt -s cdable_vars in ~/.bashrc, you can omit the $ symbol:

$ cd name 
$ pushd name

Pushd and popd also work, since bookmarks are simple variables.

Organize the bookmark:

 $ editor ~/.config/bmark/rc

Similar projects

AFAIK, my implementation (this script) is the only one which

  • integrates with bash builtin cd, pushd, popd, etc, instead of a seperate goto command
  • avoids duplicated bookmarks
  • uses basename as the default bookmark name


Flattr this

# Bookmark directories in Bash.
# by Jakukyo Friel <> under GPL v2
# repo:
# bookmark storage
mkdir -p $HOME/dotfiles/bmark
touch $bmarkdb
source $bmarkdb
bmark () {
# check if already bookmarked
if grep -m1 $PWD\" $bmarkdb; then
echo 'Already bookmarked!'
else # use basename as the default bookmark name
if [ -z $1 ]; then
bmarkname=`basename $PWD`
# check if bookmark name is already used
if grep -m1 -E "$bmarkname=" $bmarkdb; then
echo 'Bookmark name already used! Choose a different one.'
else # add bookmark
echo "$bmarkname=\"$PWD\"" >> $bmarkdb
source $bmarkdb
bmarks () {
cat $bmarkdb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment