Bash Comments and Heredoc Delimiter

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.

#linux #developer

What is GEEK

Buddha Community

Bash Comments and Heredoc Delimiter
Annalise  Hyatt

Annalise Hyatt

1594622280

Bash Heredoc and Using Heredoc with SSH

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 [tee](https://linuxize.com/post/linux-tee-command/)cat, or [sftp](https://linuxize.com/post/how-to-use-linux-sftp-command-to-transfer-files/).

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

Copy

  • The first line starts with an optional command followed by the special redirection operator << and the delimiting identifier.
  • You can use any string as a delimiting identifier, the most commonly used are EOF or END.
  • If the delimiting identifier is unquoted, the shell will substitute all variables, commands and special characters before passing the here-document lines to the command.
  • Appending a minus sign to the redirection operator <<-, 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.
  • The here-document block can contain strings, variables, commands and any other type of input.
  • The last line ends with the delimiting identifier. White space in front of the delimiter is not allowed.

Basic Heredoc Examples

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

Annalise  Hyatt

Annalise Hyatt

1595303760

Bash Select (Make Menus) and Bash select Example

In this tutorial, we will cover the basics of the select construct in Bash.

The select construct allows you to generate menus.

Bash select Construct

The select construct generates a menu from a list of items. It has almost the same syntax as the [for](https://linuxize.com/post/bash-for-loop/) loop:

select ITEM in [LIST]
do
  [COMMANDS]
done

Copy

The [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.

When the 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.

Theselectloop 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

Annalise  Hyatt

Annalise Hyatt

1594881960

Bash break and continue

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.

In Bash, break and continue statements allows you to control the loop execution.

Bash break Statement

The 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 forwhile[until](https://linuxize.com/post/bash-until-loop/), or select loop. s The syntax of the break statement takes the following form:

break [n]

Copy

[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 break.

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 2:

i=0

while [[ $i -lt 5 ]]
do
  echo "Number: $i"
  ((i++))
  if [[ $i -eq 2 ]]; then
    break
  fi
done

echo 'All Done!'

Copy

Number: 0
Number: 1
All Done!

#bash #bash break #bash continue statement

Annalise  Hyatt

Annalise Hyatt

1594213680

Writing Comments in Bash Scripts

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.

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.

Copy

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

Annalise  Hyatt

Annalise Hyatt

1594708740

Bash until Loop and Bash until Loop Example

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, [for](https://linuxize.com/post/bash-for-loop/) loop[while](https://linuxize.com/post/bash-while-loop/) loop, and until loop.

This tutorial explains the basics of the until loop in Bash.

Bash until Loop

The until loop is used to execute a given set of commands as long as the given condition evaluates to false.

The Bash until loop takes the following form:

until [CONDITION]
do
  [COMMANDS]
done

Copy

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

Copy

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

Use the [break](https://linuxize.com/post/bash-break-continue/) and [continue](https://linuxize.com/post/bash-break-continue/) statements to control the loop execution.

#bash #loop #bash until loop