3

How I can create a batch alias for bash?

I need to do this:

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

bundle and rackup - this is 2 servers

for example I want bind alias z=*all_of_this*

Scott Severance
  • 13,776
  • 9
  • 52
  • 76

2 Answers2

3

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 faye.ru -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!

Oli
  • 289,791
  • 117
  • 680
  • 835
  • 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
1

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 faye.ru -s thin -E production
}

Call it like this:

z /somefolder
Scott Severance
  • 13,776
  • 9
  • 52
  • 76