TranslateProject/sources/tech/20200109 My favorite Bash hacks.md
DarkSun 874447fbc6 选题: 20200109 My favorite Bash hacks
sources/tech/20200109 My favorite Bash hacks.md
2020-01-10 00:55:49 +08:00

5.2 KiB

My favorite Bash hacks

Improve your productivity with aliases and other shortcuts for the things you forget too often. bash logo on green background

When you work with computers all day, it's fantastic to find repeatable commands and tag them for easy use later on. They all sit there, tucked away in ~/.bashrc (or ~/.zshrc for Zsh users), waiting to help improve your day!

In this article, I share some of my favorite of these helper commands for things I forget a lot, in hopes that they will save you, too, some heartache over time.

Say when it's over

When I'm using longer-running commands, I often multitask and then have to go back and check if the action has completed. But not anymore, with this helpful invocation of say (this is on MacOS; change for your local equivalent):

function looooooooong {
    START=$(date +%s.%N)
    $*
    EXIT_CODE=$?
    END=$(date +%s.%N)
    DIFF=$(echo "$END - $START" | bc)
    RES=$(python -c "diff = $DIFF; min = int(diff / 60); print('%s min' % min)")
    result="$1 completed in $RES, exit code $EXIT_CODE."
    echo -e "\n⏰  $result"
    ( say -r 250 $result 2>&1 > /dev/null & )
}

This command marks the start and end time of a command, calculates the minutes it takes, and speaks the command invoked, the time taken, and the exit code. I find this super helpful when a simple console bell just won't do.

Install helpers

I started using Ubuntu back in the Lucid days, and one of the first things I needed to learn was how to install packages. And one of the first aliases I ever added was a helper for this (named based on the memes of the day):

`alias canhas="sudo apt-get install -y"`

GNU Privacy Guard (GPG) signing

On the off chance I have to sign a GPG email without having an extension or application to do it for me, I drop down into the command line and use these terribly dorky aliases:

alias gibson="gpg --encrypt --sign --armor"
alias ungibson="gpg --decrypt"

Docker

There are many Docker commands, but there are even more docker compose commands. I used to forget the --rm flags, but not anymore with these useful aliases:

alias dc="docker-compose"
alias dcr="docker-compose run --rm"
alias dcb="docker-compose run --rm --build"

gcurl helper for Google Cloud

This one is relatively new to me, but it's heavily documented. gcurl is an alias to ensure you get all the correct flags when using local curl commands with authentication headers when working with Google Cloud APIs. 

Git and ~/.gitignore

I work a lot in Git, so I have a special section dedicated to Git helpers.

One of my most useful helpers is one I use to clone GitHub repos. Instead of having to run:

`git clone git@github.com:org/repo /Users/glasnt/git/org/repo`

I set up a clone function:

clone(){
    echo Cloning $1 to ~/git/$1
    cd ~/git
    git clone [git@github.com][5]:$1 $1
    cd $1
}

Even though I always forget and giggle any time I'm diving into my ~/.bashrc file, I also have my "refresh upstream" command:

`alias yoink="git checkout master && git fetch upstream master && git merge upstream/master"`

Another helper for Git-ville is a global ignore file. In your git config --global --list you should see a core.excludesfile. If not, create one, and fill it full of things that you always put into your individual .gitignore files. As a Python developer on MacOS, for me this is:

.DS_Store     # macOS clutter
venv/         # I never want to commit my virtualenv
*.egg-info/*  # ... nor any locally compiled packages
__pycache__   # ... or source
*.swp         # ... nor any files open in vim

You can find other suggestions over on Gitignore.io or on the Gitignore repo on GitHub.

Your turn

What are your favorite helper commands? Please share them in the comments.


via: https://opensource.com/article/20/1/bash-scripts-aliases

作者:Katie McLaughlin 选题:lujun9972 译者:译者ID 校对:校对者ID

本文由 LCTT 原创编译,Linux中国 荣誉推出