Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

con -a = 'ssh'
con -b = 'ssh'
share|improve this question
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 ;;
  ssh username@${svr}

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
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' 
alias conb='ssh'

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


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.