I have a list of servers I often connect to, named like server001, server002... server999. I don't want to store 1000 alias hardcoded with these server names. I also don't want to write a function and take the number as the argument.

Is there a way to make alias flexible such that it takes my command input "server005" and transforms it to the commmand "ssh username@server005.example.com" without storing a list of hardcoded alias?


You could define the aliases in a loop

for l in {0..999}
  id=$(printf %03d $l)
  alias server$id="ssh user@server$id"

That will create the 1000 aliases based on the pattern

bash-4.2$ alias | grep server | head
alias server000='ssh user@server000'
alias server001='ssh user@server001'
alias server002='ssh user@server002'
alias server003='ssh user@server003'
alias server004='ssh user@server004'
alias server005='ssh user@server005'
alias server006='ssh user@server006'
alias server007='ssh user@server007'
alias server008='ssh user@server008'
alias server009='ssh user@server009'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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