robmsmt curious about speech

about | blog | github

Bash Tricks

One of the great things about bash is just how flexible and configurable it is. In this post I’ll explain some changes that I use frequently.

This is the first blog post. 🎉

This post is mostly for my own benefit, so I can quickly copy these configs onto a new machine and see how the layout styling works.

As a general rule I try not install any packages/plugins or anything which pushes you too far from the stock setup… e.g. zsh!

Bash Changes

Here are the changes I have made to my config in order of most important.


Git Branch Highlighting

I find knowing that I am in a git repo one of the most important pieces of information. On top of that it will tell me what branch I am on. This was inspired by the following post.

# git branches
git_branch() {
  git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
}
PS1="$PS1\[\033[00;31m\]\$(git_branch)\[\033[00m\]\$ "

image of commandline


Speed Aliases

# speed
alias ve="virtualenv --python=python3.8 ./venv/py38 && source ./venv/py38/bin/activate"
alias qg="git add . && git commit -m 'quick commit' && git push origin"
alias gs="git status"
alias rl="readlink -f"
alias gf='git flow'
  1. ve Typing this in any repo will generate a virtualenv and source it automatically. I use this all the time and is great when experimenting and exploring github repos. Virtualenv is supposed to be replaced now with venv.
  2. qg Stands for quick git. It’s ruining the git commit messages but it assumes you’ll squash later. Also it’s risky-ish since it’s adding everything with the git add . but each to their own.
  3. gs Stands for git status. Used in conjunction with #2. There is already a program in called gs in /usr/bin something to do with pdfs, i’ve never used before. Apparently if you want to not use the alias you can easily skip by escaping… so \gs will return the Ghost shell.
  4. rl When working with a team it’s great to send them the exact full path of the file. I use it so often and readlink -f takes too long.
  5. gf Useful if you use gitflow. gf init

Audio Aliases

# audio
alias wavcount="find -type f -name '*.wav' -print0 | xargs -0 mplayer -vo dummy -ao dummy -identify 2>/dev/null | perl -nle '/ID_LENGTH=([0-9\.]+)/ && (\$t +=\$1) && printf \"%02d:%02d:%02d\n\",\$t/3600,\$t/60%60,\$t%60' | tail -n 1"
  1. wavcount Counts the number of hours:mins:secs of audio data is in a directory recursively.
rob@rob-T480s:~/projects/audio (master)$ wavcount
06:49:34

Android Changes

When I work on Android, I find the following are useful. The first is aver which is a special alias which is exported so that it can take advantage of using a variable $1 in the middle of the command.

# android specific
function aver { aapt dump badging "$1" | grep versionName; }
export -f aver
alias sy='watch -n 2 scrcpy -m 1024 --always-on-top'
  1. aver - Stands for APK version. I can run aver apk_name.apk it prints the APK version information using the aapt tool.
rob@rob-T480s:~/$ aver spotify.apk
package: name='com.spotify.music' versionCode='65804403' versionName='8.5.82.894' platformBuildVersionName=''
  1. sy - Scrcpy (presumably pronounced Screencopy) is a great tool to see the android interface on your machine. The command sy starts scrcpy with watch -n 2 which will try to restart it every 2 secs as soon as it ends. Useful for development purposes.

Which bash configuration file to use?

To .bashrc or .profile? That is the question.

.profile is executed on startup only. .bashrc is executed every time you open a new terminal. I generally use .bashrc since I think it’s more common. I also put the following snippet into my code so I can test when each gets executed. I put this in both .bashrc and inside .profile just append it to the end.

# this prints an echo every time the script is executed (useful to know)
self=$(readlink -f "${BASH_SOURCE[0]}")
basename=$(basename "$self")
echo "$basename was executed"
rob@rob-T480s:~/projects/blog/github.io (master)$ source ~/.profile
.bashrc was executed
.profile was executed
rob@rob-T480s:~/projects/blog/github.io (master)$ source ~/.bashrc
.bashrc was executed

Useful crontabs

I want to purge my ~/Downloads folder for files and directories older than 15 days, but I want to send them to the Trash instead. The trash I want to empty at a longer every 180 days automatically on reboot only.

User: crontab -e:

0 0 * * * find /home/username/Downloads/ -mindepth 1 -mtime +15 -exec gio trash {} \;

Root: sudo crontab -e:

@reboot find /home/username/.local/share/Trash/expunged/ -type f -mtime +180 -exec rm {} \;
@reboot find /home/username/.local/share/Trash/files/ -type f -mtime +180 -exec rm {} \;
@reboot find /home/username/.local/share/Trash/info/ -type f -mtime +180 -exec rm {} \;