Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

For example, can I set

gb = cd /media/Dan/evolution

So that everytime I execute gb in bash, I can cd to that particular directory?

I find something online, alias command. But It seems can't do the work above.

Is it possible to do it? How?

share|improve this question
8  
alias gb='cd /media/Dan/evolution' –  Nykakin Jul 15 at 15:00
    
Thanks for all, I added blank around the "=" at first. –  Zen Jul 15 at 15:03
4  
@Zen, please accept the answer if it resolves the issue. This is a gesture of saying thank you to the user who helped you in resolving the issue :) –  Ramesh Jul 15 at 19:00
1  
Note: This is about bash: Linux is a kernel. Linux is often, incorrectly, used to refer to the whole operating system. This can result in confusion. For instance the question “can I do such and such thing under Linux?” is if you can do it on some computer then yes. Because you could implement any operating system you like using a Linux kernel. So we need to know which one, you are using Ubuntu Gnu+Linux+probably X11(if on a desktop ie not a server without a display). –  richard Jul 15 at 22:59
1  
@richard Indeed, that's the problem. It works since people don't mean Android and such flavors, more or less. I'm not saying it's a good thing, but it is what it is. My referral was implicit since I was commenting on your linux statement. I was just making myself clear since the subject was the confusing term. (I might've misunderstood your last comment) –  keyser Jul 17 at 16:23

4 Answers 4

up vote 47 down vote accepted

just type:

alias gb='cd /media/Dan/evolution'

To make this setting permanent (so that it sticks after you restart or open another console) add this line to the file ~/.bashrc (assuming you use the bash as your default shell)

share|improve this answer
    
To make it even more nice, you could create a dedicated alias file for your aliasses, say ~/.bash_aliases, and call it from within your ~/.bashrc file like this: [ -e ~/.bash_aliases ] && . ~/.bash_aliases This way, you don't "pollute" the ~/.bashrc file with other stuff. –  GeertVc Jul 22 at 6:38

Alternative to aliasing

gb() { cd /media/Dan/evolution; }

This defines shell function gb, which takes no arguments, and performs cd /media/Dan/evolution. As with other suggeststions, this can be added to ~/.bashrc

share|improve this answer
    
With the disadvantager that the alias command shows the defined alias, afaik defining such a function can result in "unexpected behaviour". I will vote up thou for the sake of completness :) –  vfbsilva Jul 15 at 17:34
5  
Not really. You can just type: declare -f gb to see what it does –  Paul Calabro Jul 15 at 18:54
8  
Also, this option is more flexible in that it allows you to add your custom logic within the function definition to accept arguments and do whatever you need. Aliases only allows passing arguments and doing predefined actions. –  Paul Calabro Jul 15 at 18:58
4  
@PaulCalabro Even better, type gb will work if it's an alias or a function. –  Sparhawk Jul 16 at 10:34
    
POSIXLY: { echo 'true() { : ; }' >./script ; . ./script ; command -V true ; command -V /usr/bin/true ; } - true is a shell function\n/usr/bin/true is /usr/bin/true - the \n characters were literal newlines in the output. –  mikeserv Jul 16 at 14:53

It is possible, and alias is the command you're looking for. For example alias ll="ls -l" in bash will let you type ll instead of ls -l. Please note there are no spaces used when setting an alias. man alias can be useful as well.

share|improve this answer

Alternatively, it is possible to write a C program that calls the desired command and save it to /usr/bin.

#include <stdio.h>
#include <string.h>

int main () {
    char command[50]; //notice you can change this to fit your needs
    int return_val;

    strcpy( command, "echo \"cd /media/Dan/evolution\" | bash -i" );
    return_val = system(command); 

   //this is equivalent to entering the command in a new bash, 
   //which will return and exit

    return return_val;
}

To compile it, go to the directory where the source was saved (in bash) and execute (you may need sudo):

 gcc ./gb.c -o /usr/bin/gb

After further research here, I realized simply throwing your command in system() won't work for bash built in commands. That's why you need to create an instance of an interactive bash and echo your commands to it. It's just overly complicated and probably useless, unless you want to, let's say, shorten a call to a shell script (some_script instead of sh ~/radgeRayden/Scripts/some_script).

Reference for the system() function

share|improve this answer
    
Did you actually try this? It doesn't work. –  Dennis Jul 16 at 20:17
    
No, unfortunately I'm not near my linux machine right now. But I'll try it later and report back. I've used C programs as bash aliases before, though. What didn't work exactly? –  radgeRayden Jul 16 at 20:57
    
For the same reason that sh -c "cd /media/Dan/evolution" doesn't work. Hint: subshell –  DigitalTrauma Jul 17 at 1:04
3  
cd/subshell issues aside, what is the advantage in wrapping a shell command in c program, when you could just as easily (or more easily) put that command in a shell script? –  DigitalTrauma Jul 17 at 1:10
1  
Not an expert at this, but shouldn't something like this (hopefully, something more useful) be put in /usr/local/bin? –  Joe Jul 18 at 21:38

Your Answer

 
discard

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.