Here command in linux

How to Use Here Document (HereDoc) in Linux Shell Script

While writing shell script, you might get stuck in the situation where you want to redirect a block of lines from your script to interactive commands like sed, cat, ssh, or ftp.

The purpose of this redirection might vary from situation to situation.

For example, you want to copy the block of lines from the script to a new file, replace the word, or the block of lines are commands that you need to execute in the remote system via ssh.

There is no doubt you can achieve this manually; however, “here document” will make this process way easier than you might think.

Tutorial Details

Description Here Document (HereDoc)
Difficulty Level Low
Root or Sudo Privileges No
Host System and Architecture Ubuntu 22.10 (x64)
OS Compatibility Ubuntu, Manjaro, Fedora, etc.
Prerequisites
Internet Required No
Discussed Tools in this Article

What is the Here Document (HereDoc) in Linux?

In Bash and also for other implementations of the shell, like ZSH and Fish, you can use the HereDoc to redirect a multi-line or block of lines from the script as an input to interactive commands.

The syntax for writing the HereDoc looks like the following one:

Let’s break down the above syntax:

  • [COMMAND]: The command is your target interactive command that you will use as an input for redirected multi-line or block of lines from the “HERE-DOCUMENT” location.
  • : It is the redirection symbol used to redirect anything from target (Stdout) to destination (Stdin).
  • [-]: If you append this with a redirection symbol, then all the tab spaces will be ignored during redirection, except for the normal space.
  • ‘DELIMITER’: The delimiter is like a container; anything defined between the first and last delimiter will be redirected to the target “[COMMAND]“, and if the delimiter is quoted, it will substitute all variables, commands, and special characters before passing.
  • HERE-DOCUMENT: It is the multi-line or block of lines that contain strings, variables, commands, and any other type of input that will be redirected to the “[COMMAND]“.
  • DELIMITER: The end of the ‘DELIMITER‘ container. White space in front of the delimiter is not allowed.

Note that you can use any string as a delimiter identifier; the most commonly used are EOF, END, Heredoc, Here_document, and CONTAINER.

Here Document (HereDoc) Usage

I will use the following script as an example that contains strings along with environment variables and commands to make things as easy and digestible as possible for beginners.

Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL

Copy the above lines and paste them in the script file with the name “script.sh“, and don’t forget to give executable permission to your script.

$ chmod u+x script.sh

Redirecting the Block of Lines to Cat Command

Now, if you want to redirect this whole section of code to interactive commands like “cat”, then edit the script and define the block of lines between the HereDoc.

cat Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER

If you execute the above script, all the lines between the “CONTAINER” delimiter will be substituted on the output.

$ ./script.sh Current user: linuxtldr Current Directory: /home/linuxtldr Default Shell: /bin/bash

Redirecting the Block of Lines to Cat Command Without Substituting

Enclose the “CONTAINER” delimiter with single or double quotes to prevent code substitution on the output.

cat "CONTAINER" Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER

When you run the above script, it will prevent parameter expansion and command substitution.

$ ./script.sh Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL

Redirecting the Block of Lines to Cat Command Without Tabs

If you again edit your file and include space and tabs (to avoid confusion, comments will be removed later).

The above spaces and tabs will also appear in the output.

$ ./script.sh Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL

If you want to omit the tabs (not spaces), then append the minus (““) after the redirection, as shown.

cat - "CONTAINER" Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER

Save and execute the script, and you will find tabs ignored.

$ ./script.sh Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL

Writing the HereDoc Between the For Loop Statement

If you are using the HereDoc between the for loop statement, then use the “ ” redirection symbol that will allow you to indent your code.

$ ./script.sh Line with a tab

Redirecting the Block of Lines to a Text File

Instead of printing the output on the screen, you can redirect it to a text or script file by using the “ > “, “ >> ” operators.

Note that if the file does not exist, it will be automatically created in the present directory, and “ > ” will overwrite the file and “ >> ” will append the line at the end of the file.

cat > file.txt Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER
$ ./script.sh $ cat file.txt Current user: linuxtldr Current Directory: /home/linuxtldr Default Shell: /bin/bash

Replacing the String from the Block of Lines

If you want to replace a string from the block of lines and print the output on screen, then pipe the sed command with your delimiter, as shown.

cat | sed 's/Current/Present/' Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER

The above command will replace all the “Current” strings with “Present“, as shown.

$ ./script.sh Present user: linuxtldr Present Directory: /home/linuxtldr Default Shell: /bin/bash

If you want to save the result in a text file, then redirect the output to a text file (ex: “file.txt“).

cat | sed 's/Current/Present/' > file.txt Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER
$ ./script.sh $ cat file.txt Present user: linuxtldr Present Directory: /home/linuxtldr Default Shell: /bin/bash

Writing Multi-line Comments using HereDoc

In Linux, you cannot directly specify comments for a multi-line or block of lines. The only known way is by adding the hash/pound (“ # “) sign at the beginning of each line you want to comment on.

But this method is very inconvenient when adding comments to each line and, especially, when removing them.

However, you can use the HereDoc to redirect the output to an undefined variable that will skip the multi-line or block of lines from execution, as shown.

Note : Use this method only for debugging purposes.

 Current user: $(whoami) Current Directory: $PWD Default Shell: $SHELL CONTAINER

Run the above script, and you will find all codes between “” and “CONTAINER” are skipped.

Executing a Series of Commands on the Remote System

If you spend most of the time on a remote system and repetitively execute multiple commands, you can use the HereDoc to execute a block of commands on the remote system at once.

The following is the script that defines the remote system and a series of commands that need to be executed on the target machine.

ssh -T [email protected] echo "Current user:" $(whoami) echo "Current Directory:" $PWD echo "Default Shell:" $SHELL CONTAINER

When you execute the above script, all the specified commands between the “CONTAINER” delimiter will be executed on the “[email protected]” system, as shown.

$ ./script.sh Current user: linuxtldr Current Directory: /home/linuxtldr Default Shell: /bin/bash

And that was the end of this article.

If you have any question or suggestion related to this topic, feel free to ask it in the comment section.

Источник

Here (Heredoc) в Bash

При написании сценариев оболочки вы можете оказаться в ситуации, когда вам нужно передать многострочный блок текста или кода интерактивной команде, такой как tee , cat или sftp .

В Bash и других оболочках, таких как Zsh, документ Here (Heredoc) — это тип перенаправления, позволяющий передавать команде несколько строк ввода.

Синтаксис HereDoc

Синтаксис написания HereDoc имеет следующий вид:

[COMMAND] [-] 'DELIMITER' HERE-DOCUMENT DELIMITER 
  • Первая строка начинается с необязательной команды, за которой следует специальный оператор перенаправления
  • В качестве идентификатора-разделителя можно использовать любую строку, наиболее часто используемые — EOF или END.
  • Если ограничивающий идентификатор не заключен в кавычки, оболочка заменит все переменные, команды и специальные символы перед передачей команд здесь строк документа.
  • Добавление знака минус к оператору перенаправления

Основные примеры Heredoc

В этом разделе мы рассмотрим несколько основных примеров использования heredoc.

Heredoc чаще всего используется в сочетании с командой cat .

В следующем примере мы передаем в cat две строки текста, содержащие переменную среды и команду, используя здесь документ.

cat  The current working directory is: $PWD You are logged in as: $(whoami) EOF 

Как видно из вывода ниже, подставляются как переменная, так и вывод команды:

The current working directory is: /home/linuxize You are logged in as: linuxize 

Посмотрим, что будет, если мы заключим разделитель в одинарные или двойные кавычки.

cat "EOF" The current working directory is: $PWD You are logged in as: $(whoami) EOF 

Вы можете заметить, что, когда ограничитель заключен в кавычки, оболочка не выполняет расширение параметров и подстановку команд.

The current working directory is: $PWD You are logged in as: $(whoami) 

Если вы используете heredoc внутри оператора или цикла, используйте операцию перенаправления

if true; then cat  Line with a leading tab. EOF fi 

Вместо отображения вывода на экране вы можете перенаправить его в файл с помощью операторов > , >> .

cat  file.txt The current working directory is: $PWD You are logged in as: $(whoami) EOF 

Если файл file.txt не существует, он будет создан. При использовании > файл будет перезаписан, а символ >> добавит вывод в файл.

Вход heredoc также может быть передан по конвейеру. В следующем примере команда sed заменит все экземпляры символа l на e :

Чтобы записать передаваемые данные в файл:

cat  file.txt Hello World EOF 

Использование Heredoc с SSH

Использование Heredoc — один из самых удобных и простых способов выполнения нескольких команд в удаленной системе через SSH .

При использовании разделителя без кавычек убедитесь, что вы экранировали все переменные, команды и специальные символы, иначе они будут интерполированы локально:

ssh -T [email protected]  echo "The current local working directory is: $PWD" echo "The current remote working directory is: $PWD" EOF 
The current local working directory is: /home/linuxize The current remote working directory is: /home/user 

Вы также можете настроить аутентификацию на основе ключа SSH и подключаться к своим серверам Linux без ввода пароля.

Выводы

В этом руководстве вы узнали, что такое heredoc и как использовать его в сценариях оболочки.

Если у вас есть какие-либо вопросы или отзывы, не стесняйтесь оставлять комментарии.

Источник

Читайте также:  Linux узнать адрес dhcp сервера
Оцените статью
Adblock
detector