Home » Linux » Shell

Math/Arithmetic in Bash/Shell Scripts, With Examples

Bash Math & Arithmetic

Math is easy, Bash scripting is easy, so performing math/arithmetic in Bash/Shell scripts should be easy too. It is. Here’s how to do it. Working with Integers Bash’s built-in arithmetic can only handle integer (whole number) values. If you attempt to declare a variable with a non-integer value: declare -i e=2.5 You’ll see the following: bash: declare: 2.5: syntax error: invalid arithmetic operator (error token is “.5”) To work with non-integer numbers, you will need to use an external program to perform your calculations – but first, … Read more

Home » Linux » Shell

Variables in Bash/Shell Scripts and How To Use Them [Tutorial]

Bash Variables and How to Use Them

This article will show you how to use variables in Bash and Shell scripts in Linux. Bash (or Linux Shell) scripts are files you write containing commands to automate common tasks and save yourself some time. Variables are things in scripts that hold a value for later use – a number, or a filename, or anything, really. Here’s how to define and use variables in Linux Shell scripts. These examples should work in the most popular Linux Shells, Bash, and Zsh. Declaring Bash Variables Bash VARIABLES ARE UNTYPED … Read more

Home » Linux » Shell

How to Use Functions in Bash/Shell Scripts, With Examples

Bash Function Uses

This article will explain how to use functions in Bash scripts – helping you to reuse your code and simplify your scripts. Why write code multiple times when you can write it once and re-use it? That’s what functions let you do. This article outlines the use of functions in Bash/Shell scripts. What is a Function? A function is a piece of re-usable code. Functions can accept parameters, which allow them to perform the same repeatable actions on different inputs. Functions usually perform any action, output or print results, or return … Read more

Home » Linux » Shell

Using the ‘sleep’ Function in Bash Scripts, with Examples

Bash sleep Command

This article explains the sleep command in Bash/Shell scripts and how and why you might use it. The sleep command in Bash (and other Linux Shells) pauses execution for a specified amount of time – halting the script for a set number of seconds, minutes, hours, etc. Why Pause Execution? Why would you want to pause executing your script? Give the user a chance to interrupt an automated action Await user input Wait for a device to warm up/become available Stop text from flying across the screen if you’re trying to … Read more

Home » Linux » Shell

Why You Should Wrap File Paths in Strings in Your Shell Scripts

Why You Should Wrap File Paths

I stumbled across a thread about terrible programming mistakes and found this: https://github.com/MrMEEE/bumblebee-Old-and-abbandoned/commit/a047be85247755cdbe0acce6f1dafc8beb84f2ac Which contains the line: rm -rf /foo-bar-usr /lib/nvidia-current/xorg/xorg Note – I’ve added foo-bar- to the string so that if you try to run it, it won’t do the thing I’m warning about –even having that on my clipboard makes me nervous!. What happens when it’s run? It wipes out the entire /usr/ directory – essentially bricking your computer! All because of a single space. Due to the space after /usr, the rm command interprets it as two separate directories … Read more

Home » Linux » Shell

Command Line Arguments in Shell/Bash Scripts [Tutorial]

Command Line Arguments in Bash Scripts

This tutorial explains how to pass command-line arguments to your bash scripts. There are many examples included to help get you started. Bash/Shell scripts are a great way to automate your Linux workflow and speed up your workday so you can leave the office early (or hide in the server room until 5 pm hits). Making scripts re-usable makes them more useful – you don’t need to write a script that does the same task for different sets of information or on different days – … Read more

Home » Linux » Shell

touch Command in Linux and Bash [with Examples]

touch Command in Linux

The touch command in Linux updates the timestamps on a file or creates the file if it doesn’t exist. See some examples and use cases below. It sounds useless, but it’s actually useful. For example, if you want to create an empty file called my_file.txt, you can just run: touch my_file.txt Easy! Updating the timestamps of a file is also useful. Say you have a file called favorite_tv.txt, which you use to keep the name of your current favorite TV show. Your favorite show 10 years ago was … Read more

Home » Linux » Shell

What Does “./” (Dot Slash) Mean in Linux?

What Does Dot Slash Mean in Linux

In Linux, *./ (dot slash) represents the relative path to the current working directory. This article lays out exactly what it means and how to use it. . (dot) and .. (double-dot) . (single dot) and .. (double dot) are special directory names in Linux (And other *nix operating systems). . represents the current directory. .. represents the parent directory (of the current directory). ./ (dot slash) So, the . in ./ represents the *current& directory – and the slash is the path delimiter so that what follows will refer to the contents of the current directory. Example To edit … Read more

Home » Linux » Shell

How to Install & Use Bash (Linux Shell) on Windows 10 [Tutorial]

bash for windows

Here’s how to set up and use the Linux shell on Windows – using the Windows Subsystem for Linux (WSL). This tutorial is intended for up-to-date versions of Windows 10 from 2020 onwards. I’m not going to cover how to use WSL on older versions of Windows 10 or other methods for Windows 8/7/XP/3.1 because you shouldn’t be using outdated software. If you must use Windows, use a version that still receives security patches! WSL is a great tool and allows you to pretty much run Linux software on windows … Read more

Home » Linux » Shell

What is the ‘#!’ in Linux Shell Scripts?

in Linux Shell Scripts

#! – Usually nicknamed shebang, shabang, hashbang, poundbang – we’ll stick with shebang for the duration of this article. It is found at the beginning of countless Linux shell scripts – but what actually is it? Let’s break it down. It Usually Looks Something Like This #!/bin/bash The #! appears at the beginning of the file, usually on the first line, followed by the path to an executable (in this case, the bash shell). It’s a Comment It starts with a #, so it’s not executed as part of the script itself – it does affect how the … Read more