Setting up Bash Aliases

Video loading...

  • 0:01
    Setting up Bash Aliases with Addison Berry
  • 0:09
    In this Command Line video, we're going to be looking
  • 0:10
    at Bash aliases.
  • 0:12
    Bash is the common shell that people use on Linux
  • 0:15
    and is the shell that's used on Mac,
  • 0:17
    and aliases are text shortcuts for full commands.
  • 0:23
    So that you can type 1 or 2 letters,
  • 0:25
    and have an entire command executed,
  • 0:27
    and it just saves you some extra keystrokes.
  • 0:30
    So we're going to look at how you would actually set that up,
  • 0:32
    and some examples of some common ones that
  • 0:34
    people might implement.
  • 0:37
    So the first thing that we're going to look at here is
  • 0:39
    a common example of something we might want to alias.
  • 0:42
    So a command that I type all the time
  • 0:45
    is ls -al. So I can get a detailed listing of something
  • 0:50
    in a directory.
  • 0:51
    Really handy, I use it a lot.
  • 0:53
    It can be kind of a pain.
  • 0:55
    On some systems, you'll see people type ll,
  • 0:57
    but I don't have that command and that's because that's
  • 1:00
    just a common alias that's not an actual command.
  • 1:02
    Now, if we go back up here and look at this actual listing,
  • 1:05
    I'm in my home directory and you'll see I have this
  • 1:07
    .bash_profile, and that is where I'm going
  • 1:12
    to store my alias.
  • 1:13
    This is because I'm on a Mac system.
  • 1:16
    In a regular Linux system, that's using Bash,
  • 1:19
    what you would probably want to do is put it in
  • 1:22
    a .bashrc file, so you would create a file.
  • 1:25
    Chances are this doesn't exist yet on your system,
  • 1:28
    so if it doesn't, you just create an empty
  • 1:29
    one by doing touch or Vi.
  • 1:32
    So .bashrc would be what you would commonly use on
  • 1:35
    most systems, and then bash_profile, you'd use on a Mac.
  • 1:40
    It's a lengthy discussion on why,
  • 1:41
    just know those are the filenames,
  • 1:44
    and that's where we're going to be putting stuff.
  • 1:46
    I'm on a Mac, so I'm going to put it into my bash_profile.
  • 1:51
    So I'm going to open this with Vi and
  • 1:53
    do my bash_profile in my home directory.
  • 1:56
    It's where it is because it's for my user.
  • 1:59
    You can see I already have a little bit of stuff in here,
  • 2:01
    so if one exists and there's stuff, that's fine.
  • 2:03
    I have some stuff about paths and some git sourcing.
  • 2:06
    We're going to ignore all that.
  • 2:08
    If you have an empty file, that's fine.
  • 2:11
    And I'm going to add a comment so that the #
  • 2:13
    or pound symbol means I'm doing a comment
  • 2:16
    so that I know what I'm doing here.
  • 2:17
    So this is my aliases section, and then I'm just going to
  • 2:21
    add the alias that I want.
  • 2:22
    So I type the command alias, and then the shortcut that
  • 2:26
    I want to use.
  • 2:27
    So, I'd like to use ll. You can put lsl,
  • 2:30
    you could do whatever letter combo you want, go for it,
  • 2:34
    but I like ll, because I'm used to that.
  • 2:37
    Then an equal sign, to say, "What does ll equal to?"
  • 2:40
    And then within quotes, I'm going to put the full
  • 2:42
    command that I want it to run.
  • 2:44
    So ll=ls -al. I'll do another one here,
  • 2:50
    you can see you can just keep adding them.
  • 2:51
    Go down a line.
  • 2:52
    Another common one that people do is they put in clear,
  • 2:55
    which is for clearing the screen, and so, you know,
  • 2:59
    you want to save yourself 2 letters to type,
  • 3:01
    you can put in an alias for that.
  • 3:05
    So I'm going to just list the aliases.
  • 3:07
    I'm going to save that, and now, if I try and run
  • 3:11
    my ll command, you're going to see it still doesn't work.
  • 3:15
    I've added it to my bash_profile,
  • 3:18
    but the Bash profile is only read when I start up Terminal.
  • 3:22
    So, I'm going to close this and start a new session in
  • 3:25
    my terminal and now it will read what's in
  • 3:29
    that bash_profile, and now it'll actually work.
  • 3:32
    So, now I do ll and it's actually running ls -al.
  • 3:36
    So just always remember that you need to start a new session
  • 3:42
    whenever you do this, or else it's not going to work.
  • 3:44
    So let's go back in and I'll just do a few more,
  • 3:47
    just to give you a sense of other things that people
  • 3:49
    use this for.
  • 3:50
    So, I'll create another alias and this time,
  • 3:55
    what I want to do is, it's really common
  • 3:57
    if you have common typos that you make.
  • 4:00
    So, if I, for whatever reason, always typed sodu
  • 4:04
    rather than sudo, and I did it all the time,
  • 4:06
    and then I have to go back and retype.
  • 4:08
    You can put an alias for that, and then even if you
  • 4:10
    do your regular typo you always do, it'll work.
  • 4:14
    Another thing I really like to do is to go
  • 4:16
    to certain directories.
  • 4:18
    So, I have this, like, long path or you know,
  • 4:21
    how to get to a particular directory I go to a lot.
  • 4:23
    So this is, like, my clients folder.
  • 4:25
    What I can do is just create an alias,
  • 4:28
    so that whenever I type the word clients,
  • 4:31
    the system will automatically just do cd,
  • 4:33
    the whole path and take me to that directory.
  • 4:36
    And so it's just a quick way for me to get to a common
  • 4:38
    place I go to all the time.
  • 4:42
    OK, so, I saved that, and now if I go ahead
  • 4:46
    and type in my new alias, so I created, clients to move.
  • 4:52
    So I needed to spell it correctly.
  • 4:54
    Again, it's not working, why?
  • 4:57
    Because I haven't started a new session.
  • 4:59
    I need to start a new terminal session,
  • 5:01
    in order for this to actually take effect.
  • 5:04
    So I close that, start a new session.
  • 5:06
    Now that I'm back in with a new session,
  • 5:08
    now when I type clients.
  • 5:10
    I might need to do a typo one for this, huh?
  • 5:14
    You'll see if I do a pwd now, that it actually moved me
  • 5:17
    to the clients directory.
  • 5:18
    Just like that, very simply.
  • 5:20
    And then, and speaking of the typos,
  • 5:24
    since I can't seem to spell clients right,
  • 5:25
    but we can look at if I accidentally typed
  • 5:28
    sodu, and I create a file as the root user with that,
  • 5:33
    you can see I'm being prompted just as if I would
  • 5:36
    have typed sudo. And if we look in here, with my ll,
  • 5:40
    you'll see that that file was created as root.
  • 5:43
    So it went ahead and did the sudo command even though
  • 5:45
    I typed sodu. So, those are a few different
  • 5:49
    kinds of aliases you can use, and that's all you need to
  • 5:51
    do to set an alias up for you
  • 5:53
    is do that, create that .bashrc or bash_profile, file,
  • 5:58
    and then type the alias.
  • 6:00
    The thing that you want, equals the command that
  • 6:03
    you need, save it.
  • 6:04
    Restart your terminal session and then you have all kinds
  • 6:08
    of handy shortcuts to make things even faster
  • 6:10
    for you on the command line.
Loading ...

Setting up Bash Aliases

Loading...

This video shows you how to create your own custom shortcuts for various commands. We'll look at some common aliases and see how to add them to our command line environment. This is super handy for commands that you type in all the time and don't want to go through the tedium of typing the whole thing out every time. For example, we show how to automatically go to a particular directory with just one word (e.g. type "clients" and go to the /Users/add1sun/lullabot/clients directory immediately).

Additional resources:
There are no resources for this video. If you believe there should be, please contact us.