Create Aliases in .bash_profile for Common Bash Commands

Cameron Nokes
InstructorCameron Nokes
Share this video with your friends

Social Share Links

Send Tweet
Published 6 years ago
Updated 5 years ago

.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.

Instructor: [00:00] 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.

[00:18] 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.

[00:35] 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.

[00:52] 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.

[01:05] 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.

[01:21] 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.

[01:37] 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.

[01:52] 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.

[02:09] 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.

Cameron Nokes
Cameron Nokesinstructor
~ 4 years ago

Starting with Catalina, macOS now uses zsh as the default shell instead of bash. zsh and bash are very similar, but zsh looks for a .zshrc and .zprofile instead. See for more info on the differences between the two.

Markdown supported.
Become a member to join the discussionEnroll Today