Home » Linux » Shell

How to Get the Path to the Current Bash/Shell Script

How to Get the Path to the Currently Running Bash/Shell Script in Linux

This short tutorial will demonstrate how to get the path to the currently running Bash/Shell script in Linux, and provide a code example. Getting the path to the current script is useful if you have other files in the same directory as the script that you want to access when calling the script from elsewhere. For example, you may have a script that plays music located with your music files, and have a second script on your Desktop that calls it. The first script will … Read more

Home » Linux » Shell

How to use the Bash wait Command (it’s Different to sleep)

How to use the Bash wait Command, and How it's Different to sleep

This short post will explain how to use the bash wait command to wait until a process has finished, and how if differs to the sleep command. What does the Bash wait Command Do? The wait command is a simple program that waits for a process to change state – this means waiting for it to exit. How to Use the Bash wait Command The wait command expects a list of process ids that it should wait to complete, for example: Above, the process ids 111 and 222 are supplied to wait. wait will only complete its own execution once the processes supplied to … Read more

Home » Linux » Shell

How to Check if a String is Empty in Bash Scripts

How to Check if a String is Empty in Bash Scripts

This short article will demonstrate how to check whether a string variable is empty in Bash and other Shell scripts in Linux, with examples. Bash Operators Bash operators are the symbols and expressions that perform comparisons, assignments, and arithmetic. The -z Bash Operator The -z operator returns true if a string variable is null or empty. How the use the -z Bash Operator After declaring a string variable, use the -z operator in an if statement to check whether a string is empty or not: For more Shell scripting tips, check out or Bash/Shell scripting articles!

Home » Linux » Shell

How to Grow a Linux Partition to Fill a Disk with growpart

How to Easily Grow a Linux Partition to Fill the Whole Disk using growpart

This article will show how to grow a partition on your Linux partition to fill the entire disk using growpart. This is useful if you have resized a virtual machine disk, or moved to a larger disk on your desktop or laptop Linux system. Resizing Linux Virtual Machine Disks Most commonly, you’ll be looking to grow your Linux partition to fill the entire disk on a virtual machine after resizing it. Cloud hosts like Amazon AWS and Google Cloud, and virtual machine solutions like VirtualBox and Hyper-V allow … Read more

Home » Linux » Shell

Bash Split String (+ Shell Scripts)

Bash Split String

This article will show you how to split a string at a given delimiter in Bash/Shell scripts and show some examples. Splitting strings is a handy function to have available when crafting your scripts. CSV (Comma Separated Values) is a common format in which data is made available online, where data fields in a table are separated by (surprise) commas. You may also simply be looking to split a sentence into words at the spaces, or split paragraphs into sentences at the period, and so … Read more

Home » Linux » Shell

Use wc to Count the Characters/Words/Lines [Linux/Bash]

Bash wc command count words

The wc program can be used in Bash scripts and from the Linux command line to count the number of bytes, characters, words, or lines in a file. Here’s how to use it, with examples. wc Program Syntax The syntax for the wc command is as follows: wc OPTIONS FILE Note that: OPTIONS should be provided from the below table of available options FILE is the path to the file which will have the contents counted More than one file can be specified If more than one file is specified, the total … Read more

Home » Linux » Shell

How to Use the Bash echo Command, With Examples

How to use the echo command in Bash

The Bash echo command serves a simple purpose – it outputs (echos) text. Here’s how to use it, with examples. echo Command Syntax The echo command is very simple and has the following syntax: echo OPTIONS TEXT Note that: OPTIONS should be one of the following options -n Do not output a trailing newline -e Enable interpretation of backslash escapes This means that escape characters can be used to insert special characters into the output \\ backslash \a alert (BEL) \b backspace \c produce no further output \e escape \f form feed \n new line \r carriage return \t horizontal tab \v vertical … Read more

Home » Linux » Shell

How to Use while Loops in Bash/Shell Scripts [Examples]

Bash while Loops

This article will show you how to use while loops in Bash/Shell scripts, and provides some code examples. Bash scripts let you automate tasks in the linux shell. Often, you’ll want to repeat a task for a set of data or repeated user input – that’s what while loops are for – they let you loop or iterate over a sequence of data or input, making it easy to build scripts that repeat a set of actions. The while loop syntax demonstrated below will also work for the Zsh shell and … Read more

Home » Linux » Shell

How to Use the Bash case Statement, With Examples

How to Use Bash case Statements

The case statement is used to choose which code to execute based on the value of a variable or expression. Here is how to use it in your Bash scripts. What Does the case Statement Do? The case statement is a convenient alternative to using multiple if/if else statements when you are deciding what action to take based on the value of a variable or expression. For example, if you have a variable called weekday, you can executed different code based on which day of the week is named in the variable, … Read more

Home » Linux » Shell

LinuxScrew’s Linux Shell/Bash Scripting Tips

Bash script hot tips

Here are some handy tips to keep in mind when writing your shell scripts in Linux. Shell scripts are a versatile way to automate your workflows in Linux (and MacOS, and now Windows, with the Windows Subsystem for Linux). Shell scripting syntax and behaviour does have its quirks, and there are useful shortcuts you can take as well to simplify your scripts. Bash Scripting Tips These tips are collected from around the internet, with a few of my own thrown in. Bash scripts will keep … Read more