How do I add a flag to an alias?

Is it possible to add a flag to a bash alias you create yourself? e.g.

con -a = 'ssh [email protected]'
con -b = 'ssh [email protected]'

Solution 1:

Or, use a function instead of an alias:

con() {
  local OPTIND svr
  while getopts ":ab" option; do
    case $option in
      a) svr=server1 ;;
      b) svr=server2 ;;
      ?) echo "invalid option: $OPTARG"; return 1 ;;
    esac
  done
  ssh username@${svr}.domain.com
}

con -a

Solution 2:

Nope – aliases are simple text substitutions. Use different alias names instead:

alias cona='ssh [email protected]' 
alias conb='ssh [email protected]'

EDIT if absolutely must have flags, a function will serve better than an alias – see @glenn-jackmann’s answer.