For example, can I set:

gb = cd /media/Dan/evolution

... so that every time I execute gb in bash, I can cd to that particular directory?

I found something online: the alias command. But it seems that it can't do the work above.

Is it possible to do it? How?

  • 10
    alias gb='cd /media/Dan/evolution' – Nykakin Jul 15 '14 at 15:00
  • 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 '14 at 19:00
  • 2
    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). – ctrl-alt-delor Jul 15 '14 at 22:59
  • @richard Good point, though technically this is posix complaint, so not just bash. – user60101 Jul 16 '14 at 10:23
  • 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 '14 at 16:23

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)

  • 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 '14 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

  • 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 '14 at 17:34
  • 7
    Not really. You can just type: declare -f gb to see what it does – Paul Calabro Jul 15 '14 at 18:54
  • 11
    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 '14 at 18:58
  • 5
    @PaulCalabro Even better, type gb will work if it's an alias or a function. – Sparhawk Jul 16 '14 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 '14 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.


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

  • 2
    Did you actually try this? It doesn't work. – Dennis Jul 16 '14 at 20:17
  • 1
    For the same reason that sh -c "cd /media/Dan/evolution" doesn't work. Hint: subshell – Digital Trauma Jul 17 '14 at 1:04
  • 5
    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? – Digital Trauma Jul 17 '14 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 '14 at 21:38
  • 2
    Even if this COULD work it should not go into /usr/local/bin as it is specific to the one user. – Johan Jul 29 '14 at 8:38

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.