Skip to content

Instantly share code, notes, and snippets.

@raphaelcastaneda
Created January 31, 2019 01:49
Show Gist options
  • Save raphaelcastaneda/afe1ee9669243f263248c6b511d65e7c to your computer and use it in GitHub Desktop.
Save raphaelcastaneda/afe1ee9669243f263248c6b511d65e7c to your computer and use it in GitHub Desktop.
Bash function to search lastpass cli and put passwords on your clipboard
function getpw() {
if [ $# -lt 1 ]; then
echo "Usage: getpw <LPASS_ENTRY>"
return 1
fi
options=$(lpass ls | egrep -i "$*")
count=$(echo "$options" | wc -l | sed 's/ //g')
if [ $count -gt 1 ]; then
echo "$options"
echo "Too many LastPass entries returned. Please pick from one of the above $count items."
return 2
elif [ $count -eq 0 -o -z "$options" ]; then
echo "No options returned. Please check your search and try again."
return 3
else
option_id=$(echo "$options" | awk '{print $NF}' | cut -d] -f1)
password=$(lpass show "$option_id" --password | tr -d '\n')
if [ -n "$password" ]; then
echo "Placing password for $options in to the clipboard..."
echo -n "$password" | pbcopy
else
echo "No password defined for $options, cannot copy password."
fi
fi
}
@raphaelcastaneda
Copy link
Author

I can't remember where I found this thing, but I use it all the time and I don't want to lose it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment