How to Use Bash Subshells Inside if Statements

How to Use Bash Subshells Inside if Statements

If you have ever used Bash subshells ($(...)), you know how flexible subshells can be. It only takes a few characters to start a subshell to process anything required, inline to another statement. The number of possible use cases is virtually unlimited.

We can also use Bash subshells inside if statements, inline with the statement. Doing so gives the user and developer much additional flexibility when it comes to writing Bash if statements.

If you are not familiar yet (or would like to learn more about) Bash if statements, please see our Bash If Statements: If Elif Else Then Fi article.

In this tutorial you will learn:

  • How to incorporate Bash subshells inside if statements
  • Advanced methods to incorporate Bash subshells inline with other commands
  • Examples demonstrating the use of Bash subshells in if statements

Read more

Timing bash script execution

Time Your Bash Scripts and Procedures From Inside the Code

In general, one may use the time Bash utility (see man time for more information) to run a program, and obtain runtime duration and system resource usage summaries. But how can one time particular sections of code, directly from within the Bash source code?

Using some easy variable assignments and calculations, it is possible to achieve accurate timing metrics for Bash script executions.

In this tutorial you will learn:

  • How to time Bash scripts using variable assignments and calculations
  • How to use overlapping timers to time specific sections of your scripts
  • Examples which exemplify how specific sections of code can be timed

Read more

How to Access Manual Pages for Linux Commands

How to Access Manual Pages for Linux Commands

Regular, when writing a command – both easy and complex ones – one will want to access more detailed information about the command and it’s available options. There is a wealth of information available in the Linux manual pages, and this is provided free of charge, and is available with just a few keystrokes.

In this tutorial you will learn:

  • How to access the manual page for a given command
  • How to access inline help for a given command
  • How to search all manuals for a given search term
  • How to access manual pages for builtin commands
  • How to access the manual using a GUI (graphical user interface)
  • Examples showing various manual usage use cases
How to Access Manual Pages for Linux Commands

How to Access Manual Pages for Linux Commands

Read more

How to Correctly Grep for Text in Bash Scripts

How to Correctly Grep for Text in Bash Scripts

grep is a versatile Linux utility, which can take a few years to master well. Even seasoned Linux engineers may make the mistake of assuming a given input text file will have a certain format. grep can also be used, directly in combination with if based searches to scan for the presence of a string within a given text file. Discover how to correctly grep for text independent of character sets, how to use the -q option to text for string presence, and more!

In this tutorial you will learn:

  • How to do correct character set-independent text searches with grep
  • How to use advanced grep statements from within scripts or terminal oneliner commands
  • How to test for string presence using the -q option to grep
  • Examples highlighting grep usage for these use cases

Read more

Bash Advanced Variable Idioms for Case Sensitivity Management

Bash Advanced Variable Idioms for Case Sensitivity Management

Whenever we work with textual strings, sooner or later the issue of case comes up. Does a word need to be fully uppercase, fully lowercase, with a capitalized letter at the start of the word or sentence, and so on.

An idiom is a natural language expression of a simple programming task. For example, in the sleep 10 command (which will pause the terminal one is working in for ten seconds), the word sleep is a natural language expression of what is a time based coding construct, developed in the Bash GNU coreutils software package.

There are a number of special variable-bound idioms (i.e. suffixes which can be added to a variable name, indicating what we would like to do with a given variable), which can be used in Bash to more easily do these types of conversions on the fly instead of having to use for example the Sed Stream Editor with a Regular Expression to do the same.

If you are interested in using regular expressions, have a look at our Bash Regexps For Beginners With Examples Advanced Bash Regex With Examples articles!

This makes working with variables that need case modification, or if statement testing a whole lot easier and provides great flexibility. Idioms can be added directly inside the if statements and do not need to employ a subshell with sed.

While the syntax looks slightly complex to start with, once you learn a little mental support trick to remember the right keys, you will be well on your way to use these idioms in your next script or Bash one-liner script at the command line!

In this tutorial you will learn:

  • How to use the ^, ^^, , and ,, Bash variable suffix idioms
  • How to use a regular expression [] range idiom in combination with these
  • How to use the ^ and , idioms directly from within if statements
  • Detailed examples exemplifying the use of ^, ^^, , and ,,

Read more

How to Make Decimal Calculations In Bash Using bc

How to Make Decimal Calculations In Bash Using bc

Decimal calculations are sometimes required in Bash. The standard calculation Bash programming idiom ($[]) is unable to provide a decimal output. Whilst we can trick it into calculating (but not generating) a decimal output by multiplying the numbers by for example a factor of 1000 and then doing an text based splitting, this is a ugly workaround and creates complex code. There is however a utility in Bash which can natively do decimal based calculations without any tricks or workarounds!

In this tutorial you will learn:

  • How to use bc to perform decimal calculations
  • How to make decimal based calculations at the Bash command line or from in your scripts
  • How to use variables to store the results produced by bc
  • How to use variables in further calculations
  • How to avoid Bash variable quoting errors

Read more

Terminator

Multiple Terminals Using Terminator On Linux

How about if you could have a multi-window terminal where, at will, you could press a key and it would be immediately copied to all (or a selection of) windows? How about if you could fit all terminal windows in one big window, without big and bulky borders loosing “precious” screen real estate? These, and more, are basic features of terminator, the handy Linux terminal utility.

Read more

Useful Bash Command Line Tips and Tricks Examples - Part 6

Useful Bash Command Line Tips and Tricks Examples – Part 6

In this article we have a look at obtaining some basic hardware, system and operating system configuration information directly from the command prompt in your terminal. Having this information, and knowing where to find it, often helps when configuring a system, researching optimal hardware and software solutions, and generally being in control of the Linux system you own or manage.

We will focus mostly on every day information, the kind that is required on a regular basis. Going more in-depth often requires a few online searches to first define alignment better, and then often requires additional skills to interpret the results or output shown.

For example, we will not be looking at the output of vmstat, though we will discover how to find out what CPU is in your system without opening the hood. We will also look at some basic resource limit variables and surrounding concerns. The target audience for this article is thus beginner to medium advanced.

This article is part of the Useful Bash Command Line Tips and Tricks Series.

In this tutorial you will learn:

  • Useful Bash command line tips, tricks and methods
  • How to interact with the Bash command line in an advanced manner
  • How to sharpen your Bash skills overall and become a more proficient Bash user

Read more

Big Data Manipulation for Fun and Profit Part 3

Big Data Manipulation for Fun and Profit Part 3

There have been two previous article in this series, which you may want to read first if you have not read them yet; Big Data Manipulation for Fun and Profit Part 1 and Big Data Manipulation for Fun and Profit Part 2.

In this series, we discuss various ideas and practical approaches for handling big data, or more specifically handling, transforming, mangling, munging, parsing, wrangling, transforming and manipulating the data at the Linux command line.

This third article in the series will continue explore Bash tools which can help us when processing and manipulating text-based (or in some cases binary) big data. As mentioned in the previous articles, data transformation in general is an semi-endless topic as there are hundreds of tools for each particular text format. Remember that at times using Bash tools may not be the best solution, as an off-the-shelf tool may do a better job. That said, this series is specifically for all those (many) other times when no tool is available to get your data in the format of your choice.

Finally, if you want to learn more about why big data manipulation can be both fun and profitable… please read Part 1 first.

In this tutorial you will learn:

  • Additional big data wrangling / parsing / handling / manipulation / transformation techniques
  • What Bash tools are available to assist you, specifically for text based applications
  • Various examples, showing different methods and approaches

Read more

How to Remove Bloatware From Your Samsung Android Mobile Phone

How to Remove Bloatware From Your Samsung Android Mobile Phone

Bloatware is a type of software which is installed by the product vendor (like Samsung) on top of the Android operating system in your mobile phone. But do you need all this extra software? The name clarifies; it makes your mobile bloated. Much of the utilities and services installed on your phone are simply not required, and at times may be annoying or consuming a fair bit of battery. Uninstalling a lot of these may buy you an extra day or two of battery power.

Uninstalling bloatware is made possible via ADB – the Android Debug Bridge, which is included in the stock Android SDK (Software Development Kit) and is available as an easy install on most modern Linux distributions. Setting up ADB and configuring is not the focus of this article, and you can find detailed instructions on how to do so in our How to Use ADB Android Debug Bridge to Manage Your Android Mobile Phone article.

In this tutorial you will learn:

  • How to remove bloatware from your Android mobile phone via ADB
  • What is likely safe to remove, and where you may run into problems
  • That bloatware cleanups are generally personal, tuned to your usage

Read more

How to Mirror Your Android Mobile Screen to Linux

How to Mirror Your Android Mobile Screen to Linux

Using a screen of a remote computer is often using VNC (Virtual Network Computing), or other remote desktop solutions. These come in both commercial and open source flavors. But how do you go about mirroring, and using, your Android mobile phone to and from your Linux desktop?

It is all possible via ADB – the Android Debug Bridge, which is included in the stock Android SDK (Software Development Kit) and is available as an easy install on most modern Linux distributions. Setting up ADB and configuring is not the focus of this article, and you can find detailed instructions on how to do so in our How to Use ADB Android Debug Bridge to Manage Your Android Mobile Phone article.

In this tutorial you will learn:

  • How to mirror your Android mobile phone screen to your Linux based workstation
  • How to remotely control your Android mobile phone screen via ADB
  • How to remote control your mobile phone via USB and via Wi-Fi

Read more

How to Use ADB Android Debug Bridge to Manage Your Android Mobile Phone

How to Use ADB Android Debug Bridge to Manage Your Android Mobile Phone

Mobile phones have evolved a lot over recent years, and we have seen several mobile-vs-desktop management solutions like Samsung DeX for Samsung Mobile phones, and only available for Windows 7 and 10 as well as Mac. As a non-Samsung or Linux user, one may feel left out. Not so! In fact, a whole lot more power can be provided by the ADB toolset from the Android developer team! This article will introduce you the same, and we have two followup articles which describe How to Mirror Your Android Mobile Screen to Linux and How to Remove Bloatware From Your Samsung Android Mobile Phone.

In this tutorial you will learn:

  • How to install ADB on your Linux workstation
  • How to connect your phone using a physical USB cable
  • How to connect to your phone using ADB
  • How to change the setup to enable wireless access to your phone via ADB

Read more