Scripting

Scripting

Best Practices For Managing Bash History

Controlling Bash History Storage The Bash shell stores previously entered commands in a history list which allows you to access and reuse them. However, unchecked storage of command history can use up system resources and expose sensitive information. Fortunately, Bash provides configuration options to control how much history is saved and exclude sensitive commands. Setting…

Solutions For Infinite Bash History

Storing Unlimited Bash History The default Bash shell settings limit the amount of command history that is saved between sessions. Specifically, the HISTSIZE and HISTFILESIZE variables control the number of commands stored in memory during a session and written to the ~/.bash_history file when a session ends. The default values are 500 and 1000, respectively,…

Bypassing Stdout Buffering With Unbuffer And Stdbuf

Stdout buffering refers to the storing of standard output text in a temporary memory buffer before sending it to the terminal. Shells like Bash default to block buffering stdout, which means text is collected in 4096 byte chunks before being displayed. Buffer flushing occurs during newline writes or when the buffer fills up. This buffering…

Ifs: The Input Field Separator Super-Variable In Bash

What is IFS and Why It Matters The IFS environment variable, short for Input Field Separator, controls a vital behavior in Bash – how words and lines are split into distinct entities. By default, IFS contains the space, tab, and newline characters. When Bash encounters these characters in input, it splits the input into separate…

Word Splitting Internals: How Bash And Posix Shells Handle Ifs

Understanding Word Splitting Word splitting refers to the way bash and other POSIX-compatible shells parse input strings into words. It is the process by which shells take input and break it up into discrete words that can then be processed by commands. Understanding how word splitting works is crucial for writing robust shell scripts that…

Backslash Continuation In Bash: Controlling With The -R Flag

Controlling Backslash Continuation in Bash Scripts The backslash character in Bash scripts allows wrapping long commands over multiple lines for improved readability. This technique is known as backslash continuation. Using backslash continuation structures lengthy pipelines, nested commands, and other complex logic without loss of functionality. However, unintended consequences can occur when using backslash continuation, such…

Eliminating Leading And Trailing Whitespace With Ifs In Bash Scripts

The Problem: Extra Whitespace Causing Issues in Scripts Whitespace characters like spaces, tabs, and newlines are ubiquitous in text data. Often, these characters inadvertently get introduced at the beginning or end of strings read into Bash scripts. This can cause problems when processing or passing around such values in scripts. For example, a script may…

Performance Benchmark – [ Vs [[ In Bash Conditionals

Comparing Performance of [ vs [[ in Bash Bash provides two ways of executing conditional expressions – the traditional [ command, and an improved [[ keyword added in Bash version 2.0. Understanding the differences between these can help explain the performance benchmarks outlined later in this article. [ is the original conditional expression evaluation utility…

Arithmetic Expressions In Bash – Using (( And $(( For Math

Performing Math in Bash Shell Scripts Bash provides built-in support for integer arithmetic operations through two main constructs – double parentheses (( )) and dollar-double parentheses $(( )) expressions. Both allow performing common math calculations directly inside Bash scripts and at the command line interface. Arithmetic expressions enable substitution of numeric variables and calculations into…

When To Use ( Vs [[ Vs [ – A Guide To Bash’S Conditional Syntax Forms

Deciding Between Test, Double Bracket, and Single Bracket Conditionals Explaining the key differences between (, [[, and [ Bash provides three primary methods for evaluating conditional expressions: test (()), double bracket [[]], and single bracket []. Understanding when to use each syntax form is key to writing efficient and robust Bash scripts. The test command…