From b6078d1e9be6d5e8b7d25ae1c2db45c36453403a Mon Sep 17 00:00:00 2001 From: Erik Westrup Date: Thu, 4 Nov 2021 12:42:46 +0100 Subject: [PATCH] Extract escaped_id to a function As this is now used in multiple places. --- cd-bookmark | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cd-bookmark b/cd-bookmark index b93c816..efa0dd7 100644 --- a/cd-bookmark +++ b/cd-bookmark @@ -51,6 +51,10 @@ function _cdbookmark_print_error() { echo "Try \`-h' option for more information." 1>&2 } +function _cdbookmark_escape_id() { + echo $1 | sed -e 's/[^a-zA-Z0-9<>]/\\&/g' +} + function _cdbookmark_delete_bookmark() { local bookmark_id="$1" local bookmark_dir="$(_cdbookmark_get_bookmark $bookmark_id)" @@ -60,7 +64,7 @@ function _cdbookmark_delete_bookmark() { return 4 fi - local escaped_id="$(echo $bookmark_id | sed -e 's/[^a-zA-Z0-9<>]/\\&/g')" + local escaped_id="$(_cdbookmark_escape_id $1)" sed -i '' -e "/^${escaped_id}|/d" "$BOOKMARK_FILE" } @@ -82,7 +86,7 @@ function _cdbookmark_list_bookmark_id() { } function _cdbookmark_get_bookmark() { - local escaped_id="$(echo $1 | sed -e 's/[^a-zA-Z0-9<>]/\\&/g')" + local escaped_id="$(_cdbookmark_escape_id $1)" cat "$BOOKMARK_FILE" | grep -E "^${escaped_id}\\|" | cut -d '|' -f 2 | head -n 1 }