Is it possible to define a command in bash?

16,925

Solution 1

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)

Solution 2

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

Solution 3

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:
16,925

Related videos on Youtube

Zen
Author by

Zen

Updated on September 18, 2022

Comments

  • Zen
    Zen over 1 year

    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?

    • Nicko
      Nicko almost 10 years
      alias gb='cd /media/Dan/evolution'
    • Ramesh
      Ramesh almost 10 years
      @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 :)
    • ctrl-alt-delor
      ctrl-alt-delor almost 10 years
      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).
    • Admin
      Admin almost 10 years
      @richard Good point, though technically this is posix complaint, so not just bash.
    • keyser
      keyser almost 10 years
      @richard Not "incorrectly" per se. It's a well-established term. The systems do have a lot in common, in general. Note that I'm referring to the concept of "Linux" as a bunch of OS's.
    • ctrl-alt-delor
      ctrl-alt-delor almost 10 years
      @keyser yes it is well established (Linux to mean a whole operating system), and that is a pity. Android is Linux, but can you define an alias on Android. Android and Gnu+Linux are not the some OS.
    • ctrl-alt-delor
      ctrl-alt-delor almost 10 years
      @keyser I hate to be picky, but you say as the first thing that you say on this thread (or anywhere else on Unix & Linux) “I'm referring to the concept of "Linux" as a bunch of OSs”, when did you refer?
    • keyser
      keyser almost 10 years
      @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)
    • ctrl-alt-delor
      ctrl-alt-delor almost 10 years
      +1 @keyser for “I'm not saying it's a good thing, but”, it is confusing though: when we read about Linux we don't know which meaning is being used, and may not even be aware that 2 meanings are often used.
    • ctrl-alt-delor
      ctrl-alt-delor almost 10 years
      @keyser, where you logged in as a different user before. You seem to have popped out of nowhere.
  • vfbsilva
    vfbsilva almost 10 years
    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 :)
  • Paul Calabro
    Paul Calabro almost 10 years
    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.
  • Sparhawk
    Sparhawk almost 10 years
    @PaulCalabro Even better, type gb will work if it's an alias or a function.
  • Dennis
    Dennis almost 10 years
    Did you actually try this? It doesn't work.
  • radgeRayden
    radgeRayden almost 10 years
    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?
  • Digital Trauma
    Digital Trauma almost 10 years
    For the same reason that sh -c "cd /media/Dan/evolution" doesn't work. Hint: subshell
  • Digital Trauma
    Digital Trauma almost 10 years
    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?
  • radgeRayden
    radgeRayden almost 10 years
    No advantage, really. I used it before knowing about alias and thought extra info wouldn't hurt. After further research here, I noticed the subshell thing, and the fact it exits after the command. At this point, you're just writing a shell script inside a C program.
  • radgeRayden
    radgeRayden almost 10 years
    Updated the answer to reflect the acknowledgements.
  • gokhan acar
    gokhan acar almost 10 years
    Not an expert at this, but shouldn't something like this (hopefully, something more useful) be put in /usr/local/bin?
  • radgeRayden
    radgeRayden almost 10 years
    Probably. I am not aware of many best practices, just tinker a lot with a machine I don't care very much about the integrity.
  • fatih ergin
    fatih ergin almost 10 years
    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.
  • Johan
    Johan almost 10 years
    Even if this COULD work it should not go into /usr/local/bin as it is specific to the one user.