[RFC PATCH] Add option to print the first line of an entry

Rene Kita mail at rkta.de
Mon Dec 20 12:40:26 UTC 2021


This commit enables the use of '-1' to print only the first line of an
entry. A typical use-case would be: 'password=$(pass -1 example.org)' or
'pass -1 example.org | tmux loadb -'. Before this change one had to use
'sed 1q' or similar when using multi-line entries.

Signed-off-by: Rene Kita <mail at rkta.de>
---
 man/pass.1            | 9 +++++----
 src/password-store.sh | 7 +++++--
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/man/pass.1 b/man/pass.1
index a555dcb..cd0ebf0 100644
--- a/man/pass.1
+++ b/man/pass.1
@@ -94,10 +94,11 @@ List names of passwords inside the tree that match \fIpass-names\fP by using the
 .BR tree (1)
 program. This command is alternatively named \fBsearch\fP.
 .TP
-\fBshow\fP [ \fI--clip\fP[=\fIline-number\fP], \fI-c\fP[\fIline-number\fP] ] [ \fI--qrcode\fP[=\fIline-number\fP], \fI-q\fP[\fIline-number\fP] ] \fIpass-name\fP
-Decrypt and print a password named \fIpass-name\fP. If \fI--clip\fP or \fI-c\fP
-is specified, do not print the password but instead copy the first (or otherwise specified)
-line to the clipboard using
+\fBshow\fP [ \fI-1\fP ] [ \fI--clip\fP[=\fIline-number\fP], \fI-c\fP[\fIline-number\fP] ] [ \fI--qrcode\fP[=\fIline-number\fP], \fI-q\fP[\fIline-number\fP] ] \fIpass-name\fP
+Decrypt and print a password named \fIpass-name\fP. If \fI-1\fP is specified,
+print only the first line. If \fI--clip\fP or \fI-c\fP is specified, do not
+print the password but instead copy the first (or otherwise specified) line to
+the clipboard using
 .BR xclip (1)
 or
 .BR wl-clipboard(1)
diff --git a/src/password-store.sh b/src/password-store.sh
index aef8d72..f496131 100755
--- a/src/password-store.sh
+++ b/src/password-store.sh
@@ -367,12 +367,13 @@ cmd_init() {
 
 cmd_show() {
 	local opts selected_line clip=0 qrcode=0
-	opts="$($GETOPT -o q::c:: -l qrcode::,clip:: -n "$PROGRAM" -- "$@")"
+	opts="$($GETOPT -o :1q::c::: -l qrcode::,clip:: -n "$PROGRAM" -- "$@")"
 	local err=$?
 	eval set -- "$opts"
 	while true; do case $1 in
 		-q|--qrcode) qrcode=1; selected_line="${2:-1}"; shift 2 ;;
 		-c|--clip) clip=1; selected_line="${2:-1}"; shift 2 ;;
+		-1) selected_line=1; shift ;;
 		--) shift; break ;;
 	esac done
 
@@ -383,7 +384,7 @@ cmd_show() {
 	local passfile="$PREFIX/$path.gpg"
 	check_sneaky_paths "$path"
 	if [[ -f $passfile ]]; then
-		if [[ $clip -eq 0 && $qrcode -eq 0 ]]; then
+		if [[ $clip -eq 0 && $qrcode -eq 0 && -z $selected_line ]]; then
 			pass="$($GPG -d "${GPG_OPTS[@]}" "$passfile" | $BASE64)" || exit $?
 			echo "$pass" | $BASE64 -d
 		else
@@ -394,6 +395,8 @@ cmd_show() {
 				clip "$pass" "$path"
 			elif [[ $qrcode -eq 1 ]]; then
 				qrcode "$pass" "$path"
+			else
+				echo "$pass"
 			fi
 		fi
 	elif [[ -d $PREFIX/$path ]]; then
-- 
2.30.2



More information about the Password-Store mailing list