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

con -a = 'ssh username@server1.domain.com'
con -b = 'ssh username@server2.domain.com'

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 ;;
  ssh username@${svr}.domain.com

con -a
  • Right. Though, considering OP only needs a single flag, the getopts loop might be a teensy-weensy bit overkill :). – kopischke May 10 '12 at 19:21
  • 1
    If you're going to show someone how to do something, show them the correct way. – glenn jackman May 10 '12 at 20:10
  • not contesting the merits of your answer – I linked to it indicating it is better than mine. – kopischke May 10 '12 at 20:12
  • Can this be placed in .profile along with aliases? – Ed George May 14 '12 at 11:15
  • .bashrc is usually recommended – glenn jackman May 14 '12 at 11:40

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

alias cona='ssh username@server1.domain.com' 
alias conb='ssh username@server2.domain.com'

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .