Bash Icon

Bash

Bash is a shell and command language interpreter for the GNU operating system.
29 Stories
All Topics

Shell sharats.me

Shell script best practices

An opinionated set of thumb rules that Shrikant Sharat Kandula follows after a decade of writing shell scripts. His starter template looks nice as well:

#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail
if [[ "${TRACE-0}" == "1" ]]; then
    set -o xtrace
fi

if [[ "${1-}" =~ ^-*h(elp)?$ ]]; then
    echo 'Usage: ./script.sh arg-one arg-two

This is an awesome bash script to make your life better.

'
    exit
fi

cd "$(dirname "$0")"

main() {
    echo do awesome stuff
}

main "$@"

Bash til.simonwillison.net

One-liner for running queries against CSV files with SQLite

Simon Willison figured out how to run a SQL query directly against a CSV file using the sqlite3 CLI:

sqlite3 :memory: -cmd '.mode csv' -cmd '.import taxi.csv taxi' \
  'SELECT passenger_count, COUNT(*), AVG(total_amount) FROM taxi GROUP BY passenger_count'

Ie TL;DR’d the one-liner for ya, but you’ll have to go to Simon’s site for the explainer.

Bash github.com

A collection of handy bash one-liners

Bonnie I-Man Ng:

I was working on bioinformatics a few years ago and was amazed by those single-word bash commands which are much faster than my dull scripts, time saved through learning command-line shortcuts and scripting.

Recent years I am working on cloud computing and I keep recording those useful commands here. Not all of them is oneliner, but I put effort on making them brief and swift. I am mainly using Ubuntu, Amazon Linux, RedHat, Linux Mint, Mac and CentOS, sorry if the commands don’t work on your system.

Bash github.com

A handy way to handle sh/bash CLI parameters

So Argc is a little Rust program that your bash scripts can use to make handling command-line parameters easy. You describe the options, parameters, and subcommands in comments like this:

# @describe A demo cli

# @cmd Upload a file
# @arg target!                      File to upload
upload() {
    echo "cmd                       upload"
    echo "arg:  target              $argc_target"
}

Then you trust Argc to parse that and do the heavy lifting:

eval "$(argc -e $0 "$@")"

The only drawback (aside from potential parsing bugs) to this approach is your script now depends on having argc in the machine’s executable path. i.e. – it’s not portable.

What’d be even cooler, IMHO, would be to instead use argc as a pre-processor of sorts that spits out a pure-bash implementation from your input script. Regardless, a cool idea!

Terminal arp242.net

s/bash/zsh/g

Martin Tournoij lays out a bunch of ways he finds zsh more compelling than bash.

There are many more things. I’m not going to list them all here. None of this is new; much (if not all?) of this has around for 20 years, if not longer. I don’t know why bash is the de-facto default, or why people spend time on complex solutions to work around bash problems when zsh solves them. I guess because Linux used a lot of GNU stuff and bash was came with it, and GNU stuff was (and is) using bash. Not a very good reason, certainly not one 30 years later.

Git github.com

A simple and efficient way to access various stats in a Git repo

Any git repository may contain tons of information about commits, contributors, and files. Extracting this information is not always trivial, mostly because there are a gadzillion options to a gadzillion git commands – I don’t think there is a single person alive who knows them all. Probably not even Linus Torvalds himself :).

A simple and efficient way to access various stats in a Git repo

Bash github.com

A temporary SMS utility right from your terminal 📥

tmpsms is a command line utility written in POSIX sh that allows you to get a temporary phone number and receive SMSes. It uses Upmasked temporary SMS service in order to receive the messages. This is a very useful tool for those who use are testing applications during bug bounty hunting or just need some privacy and don’t wan’t to use your personal phone number.

I don’t know when I’d ever use this, but I love that it’s POSIX compliant and depends on just a few other CLI tools (curl, jq, and fzf).

A temporary SMS utility right from your terminal 📥

Bash github.com

Dead simple testing framework for bash with coverage reporting

critic.sh exposes high level functions for testing consistent with other frameworks and a set of built in assertions. One of my most important goals was to be able to pass in any shell expression to the _test and _assert methods, so that one is not limited to the built-ins.

The coverage reporting is currently rudimentary, but it does indicate which lines haven’t been covered. It works by running the tests with extended debugging, redirecting the trace output to a log file, and then parsing it to determine which functions/lines have been executed. It can definitely be improved!

See a demo of critic.sh in action on asciinema 📽️

Jeffrey Paul sneak.berlin

Stupid unix tricks

Jeffrey Paul shares a ⛴ load of goodies. I particularly like this idea:

I have a Makefile in my home directory… that I use to store common tasks related to my local machine… The one I use most often, though, is make clean, which takes everything in ~/Desktop and moves it into ~/Documents/$YYYYMM (creating the month directory in the process if it doesn’t exist), and also empties trashes.

Reader beware: 4154 words, approximately a 23 minute read

Opensource.com Icon Opensource.com

7 Bash history shortcuts you will actually use

When people see me use these shortcuts, they often ask me, “What did you do there!?” There’s minimal effort or intelligence required, but to really learn them, I recommend using one each day for a week, then moving to the next one. It’s worth taking your time to get them under your fingers, as the time you save will be significant in the long run.

Good advice on how to adopt these. Habit formation requires persistence.

Ryan Palo assertnotmagic.com

Short circuiting in Bash

Learn from Ryan Palo about the super-powers of the “short-circuit” policy in Bash.

In Bash, the && and || (AND and OR) operators work via a “short-circuit” policy.

if [[ "$1" -gt 5 ]] && [[ "$1" -lt 10 ]]; then

This checks the first condition. If the first condition is true, then there’s a possibility the whole thing could be true, so it checks the second condition. If the second condition is true, then the whole thing is true! However, if the first condition is false, then there’s no reason to check the second condition, because the whole thing could never possibly be true with the first one false.

macOS itnext.io

macOS uses a completely outdated version of Bash

This post from Daniel Weibel not only explains how macOS uses an outdated version of Bash, but also how to upgrade to the latest Bash via Homebrew.

One thing that many macOS users don’t know is that they are using a completely outdated version of the Bash shell. However, it is highly recommended to use a newer version of Bash on macOS, because it enables you to use useful new features.

$ bash --version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)
Copyright (C) 2007 Free Software Foundation, Inc.

The reason Apple uses this old version of Bash has to do with licensing. Bash 4.0 and newer uses the GNU General Public License v3 (GPLv3), which Apple doesn’t support. There are some discussions about this on Reddit.

Version 3.2 of GNU Bash is the last version with a license that Apple is willing to accept, and so it sticks with it.

Bash github.com

A standard library and boilerplate framework for writing tools using Bash

The aim of Bash Infinity is to maximize readability of bash scripts, minimize the amount of code repeat and create a central repository for a well-written, and a well-tested standard library for bash.

It seems to me that by the time you need something as fancy/full-featured as this, maybe the task at hand has outgrown Bash? Cool, nonetheless. 👍

Bash github.com

A command-line system information tool written in bash 3.2+

The overall purpose of Neofetch is to be used in screen-shots of your system. Neofetch shows the information other people want to see. There are other tools available for proper system statistic/diagnostics.

Supports almost 150 different operating systems, so odds are it has you covered. Check my results below. Pretty decent uptime for a laptop, no?

A command-line system information tool written in bash 3.2+
Player art
  0:00 / 0:00