Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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'
share|improve this question

2 Answers 2

up vote 4 down vote accepted

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
share|improve this answer
    
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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.