Page Body

Utilizing the Shell to Maximize Productivity and Freedom

As you develop personal workflows, you may come to rely on a melange of software to accomplish tasks. Regardless of whether or not the software is Free/Libre Open Source software (FLOSS) or proprietary software, several issues may be encountered:

  • The project is no longer actively maintained or the product is dropped.
  • The software moves in a design direction that you find undesirable or, at worst, deleterious to productivity.
  • The software does not offer a feature that you know it could implement, but the project/product maintainers have chosen not to.
  • The project community no longer shares your values or engenders (what you perceive to be) an unhealthy environment.
  • The product switches to a new business model that you believe no longer justifies its cost, or you can simply no longer afford it.

Sometimes, programs for UNIX and UNIX-like systems with convenient, attractive user interfaces are using the shell to accomplish many of their tasks. Even when this is not the case, it is often possible to replicate useful features using the tools that shells offer.

Unlike many software projects and proprietary products, the Bash shell is long-lived and stable. If you care about ensuring the robustness of your workflows, it may make sense to spend time off-loading some of the work that your favorite programs accomplish to the shell.

Note: If you are not familiar with the GNU/Linux command line interface, review the Conventions page before proceeding.

The GNU/Linux Journey

Many people come to GNU/Linux via an unorganized process. Likely, they start experimenting with a GNU/Linux distribution and, at some point, decide to make it their operating system of choice.

Understandably, their approach towards building their optimal shell environment may reflect this disorder. Instead of learning everything about the Bash shell at once, they may pick up knowledge here and there, and slowly make their way towards GNU/Linux competency.

The downside of this approach is that you will likely make mistakes, pick up bad habits, and end up with a suboptimal environment. The upside is that you will learn a lot of lessons, overcome obstacles, and be able to address your mistakes to create an efficient setup.

If you are just getting started, a post like this may help give you ideas on how to leverage the shell in creative, fruitful ways.

Bash Productivity

Three substantive Bash features tied to productivity are:

  1. Aliases
  2. Functions
  3. Scripts

After you are comfortable with each of these features, you may occasionally get confused as to which one is the best tool for the job. Here are some general guidelines:

  • If you are trying to create a relatively simple command (or chain of commands) that is frequently used from an interactive shell, use an alias.
  • If you are dealing with multiple lines of commands that are frequently used from an interactive shell, and may need to support local variables and/or arguments, use a function.
  • If you are creating a series of commands that only need to be occasionally available, and may need to be available to other programs besides the shell, use a script.

Staying Organized

Over time, you may create many aliases, functions, and scripts. If you appropriately name and organize these creations, you can ensure maintainability and utilize features of the shell to help you quickly gauge what custom tools are at your disposal.

For example, use a naming prefix for your creations. Simple prefixes could be:

  • a_ for aliases
  • f_ for functions
  • s_ for scripts

Then, when you are at the command line, you can type out the prefix and press Tab, Tab (i.e., tab completion) to see all of your aliases, functions, and scripts, respectively.

Keep the contents of your shell configuration files organized. Use comments to create sections and group similar aliases/functions based on context. Add subgroup sections to your alias, function, and script names to mirror this organization (e.g., a_dirs_ for aliases that move you to specific directory locations).

Keep your scripts in a directory in your user's home directory and make sure that this location has been added to your environment's PATH.

Choose descriptive names for aliases, functions, and scripts so that you are better able to take advantage of Bash's command history options.

Use Cases

These are a few simple examples of how the Bash shell can be used for everyday tasks.

Aliases

a_updates

Check for system updates.

alias a_updates='sudo apt update && apt list --upgradeable' (Debian)

alias a_updates='dnf check-update' (Fedora)

a_size_sort

Display sizes of current directory and its content in descending order.

alias a_size_sort='du -ah -d1 . | sort -hr | less'

a_connections

Display all TCP/UDP sockets and the processes using them.

alias a_connections='ss -aptu | less'

a_processes

Display current processes using a custom output format.

alias a_processes='ps -eo comm,pid,%cpu,%mem,cls,nice,pri,ruser,euser \
    --sort=-%cpu | less'

Functions

f_up()

Update and upgrade a system.

Debian:

f_up() {
    sudo apt update &&
        sudo apt upgrade -y &&
        sudo apt-get autoremove -y &&
        sudo apt-get autoclean -y
}

Fedora:

f_up() {
    sudo dnf upgrade --refresh -y &&
        sudo dnf autoremove -y &&
        sudo dnf clean all -y
}

f_create_name()

Create a clean name for a file system object and place it on the system clipboard.

f_create_name() {
    cleaned="${1// /_}" # Replace spaces with underscores
    # Remove non-alphanumeric characters
    cleaned="${cleaned//[^a-zA-Z0-9_]/}"
    cleaned="${cleaned//[\/]/}" # Remove forward slashes
    # Lowercase name
    cleaned="$(echo -n "${cleaned}" | tr '[:upper:]' '[:lower:]')"

    echo "${cleaned}" | xclip -selection c
}

Example use:

f_create_name 'FLOSS Projects'

f_linters()

Open sites for several popular Python and shell linting resources in a system's default web browser.

f_linters() {
    # Create an array that is filled with URLs
    sites=(
        'https://flake8.pycqa.org/en/latest/user/error-codes.html'
        'https://pycodestyle.pycqa.org/en/latest/intro.html#error-codes'
        'https://www.flake8rules.com/'
        'https://gist.github.com/nicerobot/53cee11ee0abbdc997661e65b348f375'
    )

    # Read out the contents of array, pipe the output to xargs,
    # and have xargs pass one URL at a time to xdg-open
    echo "${sites[@]}" | xargs -n1 xdg-open
}

f_srch_bash()

Search online Bash resources for a provided term and open the GNU Bash Reference Manual in a system's default web browser.

f_srch_bash() {
    sites=(
        'https://www.gnu.org/software/bash/manual/html_node/index.html'
        "'https://mywiki.wooledge.org/BashGuide?action=fullsearch&context=180&value=${1}&fullsearch=Text'"
        "'https://wiki.bash-hackers.org/start?q=${1}&do=search'"

    )

    echo "${sites[@]}" | xargs -n 1 xdg-open
}

Example use:

f_srch_bash 'shell functions'

Scripts

Scripts are continually added to the content here.

The Foundation of Freedom

Investing in FLOSS helps ensure control of your digital infrastructure. Leveraging the flexibility and stability of the Bash shell is one way to employ FLOSS to create a productive digital lifestyle.

Enjoyed this post?

Subscribe to the feed for the latest updates.