Enhance Your Development Workflow with Custom Bash Functions


As developers, we often find ourselves performing repetitive tasks that, while necessary, consume a good chunk of our productive time. Fortunately, by leveraging the power of bash scripting combined with the customization capabilities of shell configuration files like .bashrc or .zshrc, we can significantly streamline our workflows.

Why Use Custom Bash Functions?

Custom bash functions act like shortcuts for executing longer or complex command sequences with a simple, memorable command. Adding these to your ~/.bashrc or ~/.zshrc configuration file means they’re always (globally) available in your terminal session, right at your fingertips. This not only saves time but also reduces the likelihood of errors that might occur when manually typing out each command.

Examples of Useful Custom Functions

Let’s dive into some practical custom function examples that you can adapt and use in your development environment.

Update All Node Dependencies

Automatically update all your npm packages in a package.json file latest versions that are compatible with the Node engine set.

autoup(){
  npm -g install npm-check-updates
  ncu --upgrade --enginesNode
}

Quick Git Checkout and Pull

Quickly checkout to a branch and ensure it’s up-to-date with the remote repository. Here, we see our first example of passing a parameter into our custom bash function.

Here is the code:

co(){
  git reset --hard HEAD
  git fetch --all
  git checkout "$1"
  git pull
}

To call this function you would simply run:

co feat/branch-to-review

Create New Branch and Set Upstream

Create a new branch, push it to remote, and set it as the upstream branch.

new(){
  git fetch --all
  git pull
  git checkout -b "$1"
  git push
  git branch -u origin/$1
}

Putting It All Together

Now let’s look at a more complex example. Can we combine two of our custom calls inside a function that takes two parameters? How do we pass those values through to the next custom function?

Combined Checkout and New Branch

Combine the checkout and new branch creation in one function for streamlined workflow.

Here’s the code:

con(){
  co "$1"
  new "$2"
}

To call this function you would simply run:

con main feat/branch-to-create

Don’t Forget! – After adding new functions, you must restart your terminal for them to be accessible!


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *