Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to write a beautifully elegant linux command in bash shell

Tags:

linux

bash

shell

I'm running several web applications in my VM which is on Ubuntu 12.04.

I find myself typing the following very often when I ssh into my VM.

  • cd /var/virtual/app1.com/
  • cd /var/virtual/app2.com/
  • cd /var/virtual/app3.com/

Each line represents a separate webapp root directory for me to perform certain instructions.

This is the lazy programmer asking if there is a way for me to type.

  • go_app1
  • go_app2
  • go_app3

I know how to do the above using bash script.

but I will end up typing

./GoApp.sh -app app1

I want to make it more elegant by simply typing go_app1.

Advice?

EDIT:

I used the top-rated answer and I got this error message in Ubuntu.

https://gist.github.com/simkimsia/7336019

Where did I go wrong?

EDIT2:

I have rewritten the error into another question here at command not found message when I try to add command in .bashrc.

like image 825
Kim Stacks Avatar asked Mar 22 '23 19:03

Kim Stacks


1 Answers

Defining an alias for each will certainly work. But since you have multiple desired shortcuts following a pattern, consider a function.

function go_app() { cd /var/virtual/app$1.com/; }

Usage:

go_app 1

Put the function definition in ~/.bashrc.

like image 178
Paul Draper Avatar answered Apr 24 '23 23:04

Paul Draper