diff two multilines bash variables to list what lines aren't in the other

 You can use the `diff` command to compare two multi-line Bash variables and list lines that are unique to each variable. Here's an example of how to do this:


```bash

#!/bin/bash


# Create two multi-line variables

variable1="apple

banana

cherry

date

elderberry"


variable2="cherry

date

fig

grape

honeydew"


# Use process substitution to pass variables to diff

diff <(echo "$variable1") <(echo "$variable2") | grep -E '^<|>'


```


In this script:


1. Two multi-line variables, `variable1` and `variable2`, are defined.

2. The `diff` command is used with process substitution (`<(...)`) to compare the contents of the two variables line by line.

3. The `diff` output is piped to `grep` to filter lines that begin with `<` (indicating lines unique to `variable1`) or `>` (indicating lines unique to `variable2`).


Running this script will list the lines that are in one variable but not in the other. In the example above, it will display:


```

< apple

< banana

< elderberry

> fig

> grape

> honeydew

```


This shows lines that are in `variable1` but not in `variable2` and vice versa. You can adjust the script to work with your own multi-line variables.

Comments

Popular posts from this blog

bad character U+002D '-' in my helm template

GitLab pipeline stopped working with invalid yaml error

How do I add a printer in OpenSUSE which is being shared by a CUPS print server?