Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

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

How I can create a batch alias for bash?

I need to do this:

cd /somefolder
bundle exec unicorn -p 3000
rackup -s thin -E production

bundle and rackup - this is 2 servers

for example I want bind alias z=*all_of_this*

share|improve this question
up vote 3 down vote accepted

Bash functions are one method but I prefer to create separate scripts for things in my ~/bin/ directory:

mkdir ~/bin/
touch ~/bin/z
chmod +x ~/bin/z
gedit ~/bin/z

Then shove your script in there (with a header) so it looks like this:

#! /bin/bash

cd "$1"
bundle exec unicorn -p 3000
rackup -s thin -E production

Then just call z <directory-path>.

It is a longer method than an alias or a bash function but I prefer it because it's a little more separable than munging things in with your other aliases. I won't blame you if you don't agree!

share|improve this answer
You should aggressively quote stuff in bash, or you'll have many problems. It'd be better to write, cd "$1" or else you won't be able to pass in a directory path containing spaces or bash special characters. – Scott Severance Dec 12 '11 at 13:04
Fair point and a trap I'm always falling into. – Oli Dec 12 '11 at 13:08
Because of that trap, my personal coding style is to treat "strings" and variables as strings in some other programming language where quotes are always required. I quote whether or not it's necessary to form the habit so I don't get caught out in situations where it matters. – Scott Severance Dec 12 '11 at 13:13

You don't want an alias; you want a bash function, which you can put in the same place as you put your aliases:

z() {
    cd "$1"   # This is the argument passed in
    bundle exec unicorn -p 3000
    rackup -s thin -E production

Call it like this:

z /somefolder
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.