DEV Community

Cover image for 8 Real-Life Shell Functions to Boost Your Productivity
Felipe Rocha
Felipe Rocha

Posted on

8 Real-Life Shell Functions to Boost Your Productivity

Introduction

Shell functions are powerful tools that allow you to automate tasks, streamline your workflow, and boost productivity by reducing repetitive manual work.

Many developers are familiar with shell scripting, but it’s not common to see them using it to enhance their productivity. Perhaps all that's missing is a bit of creativity.

In this article, I’m sharing 8 functions that I use daily to help with my work tasks. I hope they inspire you to create your own functions and boost your productivity. For the examples below, I'm using ZSH.

Image description


1: Push in Dev Environment

It's common for me to need to test my local branch in the development environment. To switch and merge branches faster, I’m currently using the following function:

function merge_dev {
  BRANCH=$(git branch --show-current);
  git checkout dev;
  git fetch -pa;
  git reset --hard origin/dev;
  git merge $BRANCH;
  echo Done! Now, finish the merge and push the changes.
}
Enter fullscreen mode Exit fullscreen mode

The git push command is intentionally left out of the merge_dev function because I often need to resolve conflicts before pushing, so I decided to keep it separate.

2: Git Rebase

Similar to Function 1, this function is useful when I need to switch branches and run another git command. To make this process faster, I created the rebase function:

function rebase {
    BRANCH=$(git branch --show-current);
    git checkout $1;
    git pull;
    git checkout $BRANCH;
    git rebase $1;
}

# usage example
rebase master
Enter fullscreen mode Exit fullscreen mode

3: Shortcut for ZSH Config Files

I’m always creating new functions or updating old ones, and I never seem to remember the path to my .zshrc files 😂.

function config {
  # Check if parameter is null
  if [ -z $1 ]; then 
    code "$HOME/.zshrc"
    return
  fi

  case "$1" in
    alias )
      code "$HOME/CUSTOM_ZSH/.alias.sh"
    ;;
    func )
      code "$HOME/CUSTOM_ZSH/.functions.sh"
    ;;
    * )
      echo "$1 is not a valid parameter for config function"
    ;;
  esac
}

# usage example
config func
Enter fullscreen mode Exit fullscreen mode

4: Set Upstream and Open Repo

It’s very annoying to try pushing a branch that doesn't exist in the remote repository. To save myself from this hassle, I use the gpp function to automatically add the --set-upstream flag when needed. I also open the remote repo in a browser because I usually create a PR after pushing a new branch.

function gpp {
  BRANCH=$(git branch --show-current);
  ORIGIN="origin"

  git push
  RESULT=$?

  if [[ $RESULT != 0 ]]; then
    GIT_PUSH_OUTPUT=$(git push --set-upstream $ORIGIN $BRANCH 2>&1)
    echo $GIT_PUSH_OUTPUT

    REPO_NAME=$(pwd | awk -F/ '{print $NF}')
    open "https://github.com/feliperocha93/$REPO_NAME"
  fi
}
Enter fullscreen mode Exit fullscreen mode

5: Fast PR

Building on Function 4 and taking advantage of ZSH aliases, when I need to open a PR with a single commit, I save time using the fast_pr function:

function fast_pr {
  gaa;
  gcmsg "$1";
  gpp;
}

# usage example
fast_pr "chore: update zsh function"
Enter fullscreen mode Exit fullscreen mode

6: Get and Export Credentials

This is probably the most complex and time-saving function in this series of 8 functions. The variables are somewhat masked since it’s not necessary to show their real names, but the example should be sufficient to explain the concept.

Basically, the application I usually run locally accesses several databases that require authentication. The application retrieves the credentials from environment variables.

To handle this, the get_credentials function authenticates me via the command line and saves the credentials as environment variables.

function get_credentials {
  # some command to get credentials
  credentials=$(foo --bar); 

  # in this example, credentials is a string like 
  # username:username password:password 
  # and awk method are extracting the value

  export USERNAME=$(echo $credentials | awk -F'username: ' '{print $2}' | tr -d '[:space:]');
  export PASSWORD=$(echo $credentials | awk -F'password: ' '{print $2}' | tr -d '[:space:]');

  export REDIS_HOST=localhost
  export REDIS_PORT=6379

  echo $USERNAME $PASSWORD
}
Enter fullscreen mode Exit fullscreen mode

7: Install Dependencies

When working on a project after a long time, it's important to update the dependencies to avoid some incompatibility problems during development. So I always try to keep my local branch up to date with master branch.

function start {
  BRANCH=$(git branch --show-current);

  function update_and_install {
    git pull;
    nvm use;
    npm ci;
  }

  if [ $BRANCH != 'master' ]; then
    git checkout master;
    update_and_install;
    git checkout $BRANCH;
    return;
  fi

 update_and_install;
}
Enter fullscreen mode Exit fullscreen mode

BONUS: Alias

I know aliases aren't exactly functions, but they can help you save some time. In the examples below, I use some aliases to execut frequent commands with short words.

# browse to folder
alias deployments="cd ~/Documents/deployments"
alias feliperocha="cd ~/Documents/feliperocha"

# browse to folder and list projects
alias projects="cd $HOME/Documents/projects && ls -a"

# open a specific folder
alias XPTO="cd ~/Documents/XPTO; open ."
Enter fullscreen mode Exit fullscreen mode

Conclusion

Thank you for reading until the end. My intetion in sharing these functions to open your mind and spark your criativity to create your own functions and boost your daily productivy.

Leave a comment below sharing which function was your favorite or post your own.

Bests regards,

Top comments (0)