#!/bin/bash die() { echo "Error: $*" >&2 exit 1 } show_default() { d=$(lpstat -d) case "$d" in 'system default destination: '*) echo "${d##* }" ;; esac } set_default() { ( set -x lpoptions -d "$1" ) } printers() { lpstat -p | while read line do case "$line" in "printer "*) p="${line#printer }" echo "${p%% *}" set -- "$p" "$line" ;; esac done } show_printers_with_default() { default_printer=$(show_default) # can be empty for p do if [ "$p" = "$default_printer" ] then echo "[default] $p" else echo " $p" fi done | nl } set -e set -- $(printers) echo show_printers_with_default "$@" echo read -p 'Choose default printer> ' i || die "Error reading input" [ "$i" ] || exit 0 [ "$i" -gt 0 ] || die "Invalid choice: $i" choice=${!i} && [ "$choice" ] || die "Invalid choice: $i" set_default $choice