1×
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.

Autoplay

    Create Aliases in .bash_profile for Common Bash Commands

    Cameron NokesCameron Nokes
    bashBash

    .bash_profile is a file that bash invokes (or more technically sources) before the start of a new bash session. In .bash_profile, we have the opportunity to add variables, functions, and aliases to customize our bash environment and provide reusable functionality.

    In this lesson, we’ll look at adding a git_sync alias as well as a ll alias. Aliases act like shortcuts and save us time and typing at the terminal. .bash_profile is a good spot for smaller functions and aliases.

    Note that on most linux distributions you'll use .bashrc instead of .bash_profile.

    Code

    Code

    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    Transcript

    Transcript

    Instructor: Bash profile has to be located in our home directory. Let's cd there, so I'll do cd~ and let's check if it exists. I'll do stat .bash_profile, mine doesn't exist so I'll go ahead and create it. Then I'm going to open it in a code editor. If yours already exists you can just edit it.

    What I want to do is make working with Git a little bit easier and involve a little less typing. I'm going to create an alias called get-sync. You declare aliases using the alias key word, and you name it, and you set it to a string. The string is what bash actually runs.

    I set this alias to git-pull with a rebase, and if that succeeds then I'll do a git-push. This will emulate the behavior that some desktop graphic GIT clients have. Aliases are basically a shortcut or abbreviation, the just make bash a little bit easier to work with and save us a little bit of typing.

    I'm going to add one more alias, I'm going to call it ll, which stands for long listing. What I'm going to do here, is I'm going to do the ls command, and I'm going to tell it to do a long listing, show hidden files, and then colorize the output.

    This sort of alias is common in bash, we're just running a command like normal, but we're providing a lot of default flags for it. Note in your bash profile you can run any kind of bash that you want. You can declare functions or variables here, and those will also get exposed to your shell, and will be usable anywhere.

    Save that, and jump back to the terminal. At this point bash doesn't know that I've made changes to it, so what we need to do is run the source command that will tell bash to evaluate the file. Then I'm going to run our ll alias, our long listing, and I'm just going to run on my applications folder.

    You can see it's working, I see the long listing, and I'm seeing hidden files come through, and I'm also seeing colorized output. That's pretty cool. Let's try our gitSync alias now. I'm going to cd into a git repository that I set up.

    If we run git-status, we can see I have one pending commit that needs to be pushed to master. Let's run getSync, OK, cool. It did the git-pull with rebase, and our current branch is up to date, so nothing happened there. Then this is the output from the git-push, so awesome, that's working too.

    Note that if we forget what our alias does we can use the type, do type gitSync, and that tells us what we set it to. Bash profile is a good spot to put small reusable aliases, functions, or variables that we want to have available to us anywhere in bash.

    Discuss

    Discuss