Search code examples
bashsedsyntaxnewlinedouble-quotes

Linux Bash variable not storing line breaks


I am working on Debian Stable Linux and I am trying to modify output of following command:

$ apt search zypper
Sorting... Done
Full Text Search... Done
libzypp-bin/stable 17.25.7-1 amd64
  openSUSE/SLES package management system library (library tools)

libzypp-common/stable,stable 17.25.7-1 all
  openSUSE/SLES package management system library (common files)

libzypp-config/stable,stable 17.25.7-1 all
  openSUSE/SLES package management system library (configuration)

libzypp-dev/stable 17.25.7-1 amd64
  openSUSE/SLES package management system library (development files)

libzypp-doc/stable,stable 17.25.7-1 all
  openSUSE/SLES package management system library (documentation)

libzypp1722/stable 17.25.7-1 amd64
  openSUSE/SLES package management system (library)

zypper/stable 1.14.42-1 amd64
  command line software manager using libzypp

zypper-common/stable,stable 1.14.42-1 all
  command line software manager using libzypp (common files)

zypper-doc/stable,stable 1.14.42-1 all
  command line software manager using libzypp (documentation)

I want to merge 2 non-blank line entries to one line with following code (from here):

$ apt search zypper | sed ':a;N;s/\n\(.\)/ \1/;ta'

WARNING: apt does not have a stable CLI interface. Use with caution in scripts.

Sorting... Full Text Search... libzypp-bin/stable 17.25.7-1 amd64   openSUSE/SLES package management system library (library tools)

libzypp-common/stable,stable 17.25.7-1 all   openSUSE/SLES package management system library (common files)

libzypp-config/stable,stable 17.25.7-1 all   openSUSE/SLES package management system library (configuration)

libzypp-dev/stable 17.25.7-1 amd64   openSUSE/SLES package management system library (development files)

libzypp-doc/stable,stable 17.25.7-1 all   openSUSE/SLES package management system library (documentation)

libzypp1722/stable 17.25.7-1 amd64   openSUSE/SLES package management system (library)

zypper/stable 1.14.42-1 amd64   command line software manager using libzypp

zypper-common/stable,stable 1.14.42-1 all   command line software manager using libzypp (common files)

zypper-doc/stable,stable 1.14.42-1 all   command line software manager using libzypp (documentation)

It works all right. Now I have to store above output in variable in a bash script file and echo it later with following code:

var=`apt search zypper | sed ':a;N;s/\n\(.\)/ \1/;ta'`
echo $var 

However, output of above code is:

Sorting... Full Text Search... libzypp-bin/stable 17.25.7-1 amd64 openSUSE/SLES package management system library (library tools) libzypp-common/stable,stable 17.25.7-1 all openSUSE/SLES package management system library (common files) libzypp-config/stable,stable 17.25.7-1 all openSUSE/SLES package management system library (configuration) libzypp-dev/stable 17.25.7-1 amd64 openSUSE/SLES package management system library (development files) libzypp-doc/stable,stable 17.25.7-1 all openSUSE/SLES package management system library (documentation) libzypp1722/stable 17.25.7-1 amd64 openSUSE/SLES package management system (library) zypper/stable 1.14.42-1 amd64 command line software manager using libzypp zypper-common/stable,stable 1.14.42-1 all command line software manager using libzypp (common files) zypper-doc/stable,stable 1.14.42-1 all command line software manager using libzypp (documentation)

All the lines have merged together in one long line. I need to preserve newline characters so that lines remain separate.

Where is the problem and how can it be solved?


Solution

  • Solution: echo "$var"

    Reason: The contents of $var are replaced, one character at a time, by the character strings invoking echo, based on the newer of their two values. The Internal Field Separator (IFS) is a character that separates the fields of a multibyte or wide-character string. In a Bash shell, the IFS is set to whitespace by default, so the shell chops your string into arguments and it never gets to see the newline. This is because the shell considers they are separators just like a space.