Skip to main content
Fixed several bugs in code. Added note about tab completion that only works in newer versions of bash.
Source Link

Bash variables can be used to create a bookmarking system. VariablesVariables will work with any command and bash will tab complete both the variable name and. In newer versions of bash, if a / is appended to the variable name, the path that the variable contains can be tab completed as well.

mydir=/home/chris/dir
ls $my         #tab# Tab completion works on variable name.
ls $mydir/     #tab# Tab completion is equivalent to that with ls /home/chris/dir/
               # (doesn't work in older versions of bash).

For persistence, variable declarations can be kept in a file that is sourced from .bashrc. Since this file is a bash script, it can contain declarations that reference other variables, such as aur="${HOME}/AUR", or that only run on certain hosts if [[ $(hostname)$HOSTNAME == foo ]]; then bar=baz; fi, which is useful if you like to reuse config files across multiple hosts and users.

The following is a bash function (to be added to .bashrc, or sourced from it) that allows bookmarks to be added to and removed from the bookmarks file. It is fairly new and not guaranteed to be free of bugs.

bookmark_file=~/.bookmarks
source "$bookmark_file"

bm() {
usage='Usage:
bm add <name> <path>           Create a bookmark for path.
bm add <name>                  Create a bookmark for the current directory.
bm update                      Source the bookmark file.
bm remove <name>               Remove a bookmark'              

case $1 in
    add)
        local path
        if [[ $# -eq 2 ]]; then
            path=.
        elif [[ $# -eq 3 ]]; then
            path="$3"if [[ -e $3 ]]; then
        fi
        ifpath="$3"
 [[ -n $path ]]; then       else
            if [[ -n $(eval echo \$$2)"bm: ]];${3}: thenNo such file or directory."
                echoreturn "The1
 variable name $2 is in use."      fi               
        else
            echo "$usage"
            return 1
        fi
     fi   
        if declare | grep path=$(readlink"^${2}=" -f> "$path")/dev/null; then
            echo ${2}=\""$path"\""bm: >>The "$bookmark_file"name $2 is in use."
            evalreturn 1
        fi
        path=$(readlink -f "$path")
        echo ${2}=\""$path"\"
   >> "$bookmark_file"
        returneval 0${2}=\""$path"\"
        fireturn 0
        ;;
    update)
        if [[ $# -eq 1 ]]; then
            source "$bookmark_file"
            return 0
        fi
        ;;
    remove)
        if [[ $# -eq 2 ]]; then
            unset $2
            local contents=$(grep -v $"^${2}==" "$bookmark_file")
            echo "$contents" > "${bookmark_file}.tmp"
            rm -f "$bookmark_file"
            mv "${bookmark_file}.tmp" "$bookmark_file"
            return 0
        fi
        ;;
esac
        
echo "$usage"
return 1
}

Bash variables can be used to create a bookmarking system. Variables will work with any command and bash will tab complete both the variable name and, if a / is appended to the name, the path that the variable contains.

mydir=/home/chris/dir
ls $my         #tab completion works on variable name
ls $mydir/     #tab completion is equivalent to that with ls /home/chris/dir/

For persistence, variable declarations can be kept in a file that is sourced from .bashrc. Since this file is a bash script, it can contain declarations that reference other variables, such as aur="${HOME}/AUR", or that only run on certain hosts if [[ $(hostname) == foo ]]; then bar=baz; fi, which is useful if you like to reuse config files across multiple hosts and users.

The following is a bash function (to be added to .bashrc, or sourced from it) that allows bookmarks to be added to and removed from the bookmarks file. It is fairly new and not guaranteed to be free of bugs.

bookmark_file=~/.bookmarks
source "$bookmark_file"

bm() {
usage='Usage:
bm add <name> <path>           Create a bookmark for path.
bm add <name>                  Create a bookmark for the current directory.
bm update                      Source the bookmark file.
bm remove <name>               Remove a bookmark'              

case $1 in
    add)
        local path
        if [[ $# -eq 2 ]]; then
            path=.
        elif [[ $# -eq 3 ]]; then
            path="$3"
        fi
        if [[ -n $path ]]; then
            if [[ -n $(eval echo \$$2) ]]; then
                echo "The variable name $2 is in use."
                return 1
            fi
            path=$(readlink -f "$path")
            echo ${2}=\""$path"\" >> "$bookmark_file"
            eval ${2}=\""$path"\"
            return 0
        fi
        ;;
    update)
        if [[ $# -eq 1 ]]; then
            source "$bookmark_file"
            return 0
        fi
        ;;
    remove)
        if [[ $# -eq 2 ]]; then
            unset $2
            local contents=$(grep -v ${2}= "$bookmark_file")
            echo "$contents" > "${bookmark_file}.tmp"
            rm -f "$bookmark_file"
            mv "${bookmark_file}.tmp" "$bookmark_file"
            return 0
        fi
        ;;
esac
        
echo "$usage"
return 1
}

Bash variables can be used to create a bookmarking system. Variables will work with any command and bash will tab complete the variable name. In newer versions of bash, if a / is appended to the variable name, the path the variable contains can be tab completed as well.

mydir=/home/chris/dir
ls $my         # Tab completion works on variable name.
ls $mydir/     # Tab completion is equivalent to that with ls /home/chris/dir/
               # (doesn't work in older versions of bash).

For persistence, variable declarations can be kept in a file that is sourced from .bashrc. Since this file is a bash script, it can contain declarations that reference other variables, such as aur="${HOME}/AUR", or that only run on certain hosts if [[ $HOSTNAME == foo ]]; then bar=baz; fi, which is useful if you reuse config files across multiple hosts and users.

The following bash function (to be added to .bashrc, or sourced from it) allows bookmarks to be added to and removed from the bookmarks file. It is fairly new and not guaranteed to be free of bugs.

bookmark_file=~/.bookmarks
source "$bookmark_file"

bm() {
usage='Usage:
bm add <name> <path>           Create a bookmark for path.
bm add <name>                  Create a bookmark for the current directory.
bm update                      Source the bookmark file.
bm remove <name>               Remove a bookmark'              

case $1 in
    add)
        local path
        if [[ $# -eq 2 ]]; then
            path=.
        elif [[ $# -eq 3 ]]; then
            if [[ -e $3 ]]; then
                path="$3"
            else
                echo "bm: ${3}: No such file or directory."
                return 1
            fi               
        else
            echo "$usage"
            return 1
        fi
        
        if declare | grep "^${2}=" > /dev/null; then
            echo "bm: The name $2 is in use."
            return 1
        fi
        path=$(readlink -f "$path")
        echo ${2}=\""$path"\" >> "$bookmark_file"
        eval ${2}=\""$path"\"
        return 0
        ;;
    update)
        if [[ $# -eq 1 ]]; then
            source "$bookmark_file"
            return 0
        fi
        ;;
    remove)
        if [[ $# -eq 2 ]]; then
            unset $2
            local contents=$(grep -v "^${2}=" "$bookmark_file")
            echo "$contents" > "${bookmark_file}.tmp"
            rm -f "$bookmark_file"
            mv "${bookmark_file}.tmp" "$bookmark_file"
            return 0
        fi
        ;;
esac
        
echo "$usage"
return 1
}
Source Link

Bash variables can be used to create a bookmarking system. Variables will work with any command and bash will tab complete both the variable name and, if a / is appended to the name, the path that the variable contains.

mydir=/home/chris/dir
ls $my         #tab completion works on variable name
ls $mydir/     #tab completion is equivalent to that with ls /home/chris/dir/

For persistence, variable declarations can be kept in a file that is sourced from .bashrc. Since this file is a bash script, it can contain declarations that reference other variables, such as aur="${HOME}/AUR", or that only run on certain hosts if [[ $(hostname) == foo ]]; then bar=baz; fi, which is useful if you like to reuse config files across multiple hosts and users.

The following is a bash function (to be added to .bashrc, or sourced from it) that allows bookmarks to be added to and removed from the bookmarks file. It is fairly new and not guaranteed to be free of bugs.

bookmark_file=~/.bookmarks
source "$bookmark_file"

bm() {
usage='Usage:
bm add <name> <path>           Create a bookmark for path.
bm add <name>                  Create a bookmark for the current directory.
bm update                      Source the bookmark file.
bm remove <name>               Remove a bookmark'              

case $1 in
    add)
        local path
        if [[ $# -eq 2 ]]; then
            path=.
        elif [[ $# -eq 3 ]]; then
            path="$3"
        fi
        if [[ -n $path ]]; then
            if [[ -n $(eval echo \$$2) ]]; then
                echo "The variable name $2 is in use."
                return 1
            fi
            path=$(readlink -f "$path")
            echo ${2}=\""$path"\" >> "$bookmark_file"
            eval ${2}=\""$path"\"
            return 0
        fi
        ;;
    update)
        if [[ $# -eq 1 ]]; then
            source "$bookmark_file"
            return 0
        fi
        ;;
    remove)
        if [[ $# -eq 2 ]]; then
            unset $2
            local contents=$(grep -v ${2}= "$bookmark_file")
            echo "$contents" > "${bookmark_file}.tmp"
            rm -f "$bookmark_file"
            mv "${bookmark_file}.tmp" "$bookmark_file"
            return 0
        fi
        ;;
esac
        
echo "$usage"
return 1
}