1- #! /usr/bin/env bash
1+ # shellcheck shell= bash
22# Bash completion support for ssh.
33
44export COMP_WORDBREAKS=${COMP_WORDBREAKS/ \: / }
55
66_sshcomplete () {
7- local CURRENT_PROMPT=" ${COMP_WORDS[COMP_CWORD]} "
7+ local line CURRENT_PROMPT=" ${COMP_WORDS[COMP_CWORD]} "
88 if [[ ${CURRENT_PROMPT} == * @* ]]; then
99 local OPTIONS=" -P ${CURRENT_PROMPT/@*/ } @ -- ${CURRENT_PROMPT/*@/ } "
1010 else
@@ -17,20 +17,21 @@ _sshcomplete() {
1717 | awk ' {for (i=2; i<=NF; i++) print $i}' \
1818 | sed -Ee " s|^([^/~])|$HOME /.ssh/\1|" -e " s|^~/|$HOME /|" ) ; do
1919 if [ -r " $fl " ]; then
20- COMPREPLY=(${COMPREPLY[@]} $( compgen -W " $( grep -i ^Host " $fl " | grep -v ' [*!]' | awk ' {for (i=2; i<=NF; i++) print $i}' ) " ${OPTIONS} ) )
20+ # shellcheck disable=SC2086
21+ while IFS=' ' read -r line; do COMPREPLY+=(" $line " ); done < <( compgen -W " $( grep -i ^Host " $fl " | grep -v ' [*!]' | awk ' {for (i=2; i<=NF; i++) print $i}' ) " ${OPTIONS} )
2122 fi
2223 done
2324
2425 # parse all hosts found in .ssh/known_hosts
25- if [ -r " $HOME /.ssh/known_hosts" ]; then
26- if grep -v -q -e ' ^ ssh-rsa' " $HOME /.ssh/known_hosts" ; then
27- COMPREPLY=(${COMPREPLY[@]} $( compgen -W " $( awk ' {print $1}' " $HOME /.ssh/known_hosts" | grep -v ^\| | cut -d, -f 1 | sed -e ' s/\[//g' | sed -e ' s/\]//g' | cut -d: -f1 | grep -v ssh-rsa) " ${OPTIONS} ) )
28- fi
26+ if [ -r " $HOME /.ssh/known_hosts" ] && grep -v -q -e ' ^ ssh-rsa' " $HOME /.ssh/known_hosts" ; then
27+ # shellcheck disable=SC2086
28+ while IFS=' ' read -r line; do COMPREPLY+=(" $line " ); done < <( compgen -W " $( awk ' {print $1}' " $HOME /.ssh/known_hosts" | grep -v ^\| | cut -d, -f 1 | sed -e ' s/\[//g' | sed -e ' s/\]//g' | cut -d: -f1 | grep -v ssh-rsa) " ${OPTIONS} )
2929 fi
3030
3131 # parse hosts defined in /etc/hosts
3232 if [ -r /etc/hosts ]; then
33- COMPREPLY=(${COMPREPLY[@]} $( compgen -W " $( grep -v ' ^[[:space:]]*$' /etc/hosts | grep -v ' ^#' | awk ' {for (i=2; i<=NF; i++) print $i}' ) " ${OPTIONS} ) )
33+ # shellcheck disable=SC2086
34+ while IFS=' ' read -r line; do COMPREPLY+=(" $line " ); done < <( compgen -W " $( grep -v ' ^[[:space:]]*$' /etc/hosts | grep -v ' ^#' | awk ' {for (i=2; i<=NF; i++) print $i}' ) " ${OPTIONS} )
3435 fi
3536
3637 return 0
0 commit comments