Bash is an underrated power tool in most developers toolbox. Here's a few ways to make it work for you.
I've got a short, simple Bash profile on each of my machines that helps me get my job done. It's low-friction with easy to remember commands set up that not only save me a few keystrokes, but make things a little more fun, too. Today, I'm going to go over a few of them and talk a bit about why I set them up and ways you can customize them for your own use.
Before I start breaking my setup down for you, if you've never used a Bash command before, I'll direct you to a few useful links to help get you started:
While writing Bash in the command line is useful on its own, I find it most powerful when I set up commands in a
.bash_profile file. This is where the magic starts!
Okay! Now that you have a bit more knowledge about Bash in general, I'll show you my favorite uses for the language: variables and creating aliases!
In Bash, variables can be used to point to various folders and make the functions we write more DRY. They're defined without a
$ at assignment, but are called with
$ when used.
mainrepo='main-repo-name' $ cd $mainrepo // navigates to "main-repo-name"
Distinct from variables, but with similar usage, are aliases. They can contain commands as well as paths, variables, and function calls. They can also contain other aliases!
alias dev="npm run dev" $ dev // runs the "npm run dev" command
I use aliases to build up a set of tools that make navigating folders, opening applications, and launching local environments much easier for myself.
I'll outline an example here to show you how this might work.
alias cuppa="code $HOME/Projects/cuppa"
This will create an alias called "cuppa" (the name of my current side project) and when I run that command, it will open the project folder in VS Code for me. Note that
$HOME variable? That comes for free on Unix-based environments and points to the current user's home folder.
Okay, so that opens my project files into my text editor, but what about the terminal itself? I have an alias for that as well:
alias current="cd; cd Projects/cuppa"
Here the alias called "current" is set to do two things (it's starting to get magical now!). First it will
cd into the home folder to make sure the shell doesn't get confused on where to look for the folder. Then it will navigate via
cd Projects/cuppa so that folder is currently active.
Once I'm inside that folder, I've got aliases that help me run my npm scripts more efficiently:
alias coverage="npm run coverage" alias dev="npm run dev"
Here I'm using the alias
coverage to enable me to run my Jest tests with coverage reports. In this case we've got some alias inception going on because
coverage is a script in my package.json that is a shortcut for
jest --coverage --colors.
npm run dev is a shortcut for
concurrently \"npm run server\" \"npm run client\" and inside that, even
client are shortcuts!
Any of these can be set up as Bash aliases as well. To give an example of what we're saving in commands and keystrokes, here's what I would have to type in the command line to get my Cuppa app up and running without aliases:
$ code $HOME/Projects/cuppa $ cd $ cd Projects/cuppa $ concurrently $ nodemon server/server.js $ webpack-dev-server --config ./webpack.config.js --mode development
With my aliases setup, this is what I type instead:
$ cuppa $ current $ start
Hopefully you've seen some of the power of Bash and aliases. But when I look at that shortcut setup, I start wondering if I can make this even easier. Well, we can string aliases into a new alias! It looks something like this:
alias goodmorning="cuppa; current; start;" $ goodmorning
Now when I type the command
goodmorning I tell my shell to open my project in VS Code, navigate into the project folder, and start the local server up! With just a single word (you can even make an alias a single letter!) I'm up and running and ready to start working. MAGIC!
; between each command. That's essential so the shell doesn't try to run the entire alias as a single command. The semi-colon takes the place of the enter key in this case.
I have a few tips for creating a Bash profile:
- Don't forget to reload your profile whenever you make changes! If you're wondering why an alias isn't working, it's possibly because the shell needs to reload to reflect your changes. As you might expect by now, I've created an alias to help me with this!
alias reload=". ~/.bash_profile"
- Keep your alias names simple and easy to remember. Having an arcane system that you don't remember off the top of your head will make this considerably less useful.
- Don't just limit yourself to folder navigation. You can use aliases to set settings, start servers, and do other powerful things.
- You can update the aliases as often as you'd like. I have a
currentalias for my most often opened current project, but when that changes, I'll update my profile to reflect the new project. Variables are a great way to hold this bit of information to be used across aliases.
- Most importantly, figure out what's going to be most useful for you. There's no reason to copy any of my examples if your workflow is different from mine.
Here are a few more examples of what's possible with your Bash profile:
alias simple="python -m SimpleHTTPServer" $ simple 8000 // This opens a local web server at port 8000
alias short='export PS1="\\$\[\e[36m\]\W\[\e[m\]\[\e[35m\]\`parse_git_branch\`\[\e[m\] "'
Note that there's a call to a function
parse_git_branch. While I didn't go into functions in this article, they're a powerful way of making your Bash profile even more useful!
This can be run to open your bash file for easy editing. Change "code" to your choice of IDE (code opens VS Code)
alias open="code $HOME/.bash_profile"
echo '************************************************' echo '************* Bash Profile Sourced *************'
While this isn't an alias (the
echo command only writes its value to the command line) it's useful in confirming that changes made to the profile have been loaded.
alias showhidden="defaults write com.apple.finder AppleShowAllFiles YES" alias hidehidden="defaults write com.apple.finder AppleShowAllFiles FALSE"
On the Mac, option + click on the finder icon and relaunch finder for this to take effect.
I hope you can see the possibilities here are limitless. I personally think Bash automation is one of the most underrated skills in software development and something we can use to make our dev time more efficient so we can get on with the fun stuff.
I've only showed you a few of the ways I personally use my Bash profile to make my day run more smoothly. If you'd like to see more, you can check out evolvingbash, my open-sourced bash profile.
Now go forth and make use of that magical terminal!