In this video, we have discussed bash comments and heredoc delimiter. Both concepts have been explained briefly. Be sure to watch the full video for understanding these concepts.
When writing shell scripts you may be in a situation where you need to pass a multiline block of text or code to an interactive command, such as
In Bash and other shells like Zsh, a Here document (Heredoc) is a type of redirection that allows you to pass multiple lines of input to a command.
The syntax of writing HereDoc takes the following form:
[COMMAND] <<[-] 'DELIMITER' HERE-DOCUMENT DELIMITER
<<and the delimiting identifier.
<<-, will cause all leading tab characters to be ignored. This allows you to use indentation when writing here-documents in shell scripts. Leading whitespace characters are not allowed, only tab.
In this section, we will look at some basic examples of how to use heredoc.
Heredoc is most often used in combination with the cat command.
#bash #bash heredoc #ssh
In this tutorial, we will cover the basics of the
select construct in Bash.
select construct allows you to generate menus.
select construct generates a menu from a list of items. It has almost the same syntax as the
select ITEM in [LIST] do [COMMANDS] done
[LIST] can be a series of strings separated by spaces, a range of numbers, output of a command, an array, and so on. A custom prompt for the
select construct can be set using the
PS3 environment variable.
select construct is invoked, each item from the list is printed on the screen (standard error), preceded with a number.
If the user enters a number that corresponds to the number of one of the displayed items, then the value of
[ITEM] is set to that item. The value of the selected item is stored in the variable
REPLY. Otherwise, if the user input is empty, the prompt and the menu list are displayed again.
selectloop will continue to run and prompt for user input until the
[break](https://linuxize.com/post/bash-break-continue/)command is executed.To demonstrate how the
select construct works, let’s take a look at the following simple example:
PS3="Enter a number: " select character in Sheldon Leonard Penny Howard Raj do echo "Selected character: $character" echo "Selected number: $REPLY" done
#bash #bash select construct #bash select
Loops allow you to run one or more commands multiple times until a certain condition is met. However, sometimes you may need to alter the flow of the loop and terminate the loop or only the current iteration.
continue statements allows you to control the loop execution.
break statement terminates the current loop and passes program control to the command that follows the terminated loop. It is used to exit from a
select loop. s The syntax of the
break statement takes the following form:
[n] is an optional argument and must be greater than or equal to 1. When
[n] is provided, the n-th enclosing loop is exited.
break 1 is equivalent to
To better understand how to use the
break statement, let’s take a look at the following examples.
In the script below, the execution of the
[while](https://linuxize.com/post/bash-while-loop/) loop will be interrupted once the current iterated item is equal to
i=0 while [[ $i -lt 5 ]] do echo "Number: $i" ((i++)) if [[ $i -eq 2 ]]; then break fi done echo 'All Done!'
Number: 0 Number: 1 All Done!
#bash #bash break #bash continue statement
When writing Bash scripts, it is always a good practice to make your code clean and easily understandable. Organizing your code in blocks, indenting, giving variables and functions descriptive names are several ways to do this.
Another way to improve the readability of your code is by using comments. A comment is a human-readable explanation or annotation that is written in the shell script.
Adding comments to your Bash scripts will save you a lot of time and effort when you look at your code in the future. Let’s say you want to change a script that you wrote a few months or years ago. The chances are that you will not remember why you wrote some complicated piece of code unless you added a comment.
The comments also help other developers and system administrators who may need to maintain the script to understand your code and its purpose.
Comments are used to explain the code. For example, if you have a complex regex or parameter substitution inside your Bash script, you should write a comment describing what the code does. Comments should be short and to the point. Do not explain something which is simple and obvious to the reader.
In this article, we’ll cover the basics of writing comments in Bash.
Bash ignores everything written on the line after the hash mark (
#). The only exception to this rule is when the first line on the script starts with the
#! characters. This sequence of characters is called Shebang and is used to tell the operating system which interpreter to use to parse the rest of the file.
Comments can be added at the beginning on the line or inline with other code:
# This is a Bash comment. echo "This is Code" # This is an inline Bash comment.
The blank space after the hash mark is not mandatory, but it will improve the comment’s readability.
If your text editor supports syntax highlighting, comments are usually represented in green.
Comments are also useful when testing a script. Instead of deleting some lines or blocks, you can comment them out:
# if [[ $VAR -gt 10 ]]; then # echo "Variable is greater than 10." # fi
#bash #bash scripts #terminal
Loops are one of the fundamental concepts of programming languages. Loops are handy when you want to run a series of commands over and over again until a specific condition is met.
In scripting languages such as Bash, loops are useful for automating repetitive tasks. There are 3 basic loop constructs in Bash scripting,
[while](https://linuxize.com/post/bash-while-loop/) loop, and
This tutorial explains the basics of the
until loop in Bash.
until loop is used to execute a given set of commands as long as the given condition evaluates to false.
until loop takes the following form:
until [CONDITION] do [COMMANDS] done
The condition is evaluated before executing the commands. If the condition evaluates to false, commands are executed. Otherwise, if the condition evaluates to true the loop will be terminated and the program control will be passed to the command that follows.
In the example below, on each iteration the loop prints the current value of the variablecounterandincrements the variableby one.#!/bin/bash counter=0 until [ $counter -gt 5 ] do echo Counter: $counter ((counter++)) done
The loop iterates as long as the
counter variable has a value greater than four. The script will produce the following output:
Counter: 0 Counter: 1 Counter: 2 Counter: 3 Counter: 4 Counter: 5
#bash #loop #bash until loop