Skip to main content
replaced http://unix.stackexchange.com/ with https://unix.stackexchange.com/
Source Link

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

As StephaneStephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:

$ help set 
...
      -f  Disable file name generation (globbing).

Otherwise:

$ cd /; a=( $(printf "*\n") )
$ echo ${a[@]}
bin boot cdrom dev etc home ...

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:

$ help set 
...
      -f  Disable file name generation (globbing).

Otherwise:

$ cd /; a=( $(printf "*\n") )
$ echo ${a[@]}
bin boot cdrom dev etc home ...

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:

$ help set 
...
      -f  Disable file name generation (globbing).

Otherwise:

$ cd /; a=( $(printf "*\n") )
$ echo ${a[@]}
bin boot cdrom dev etc home ...
added 500 characters in body
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:

$ help set 
...
      -f  Disable file name generation (globbing).

Otherwise:

$ cd /; a=( $(printf "*\n") )
$ echo ${a[@]}
bin boot cdrom dev etc home ...

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

As Stephane notes, when subjecting the output of a command to further shell expansion, one should disable globbing using set -f:

$ help set 
...
      -f  Disable file name generation (globbing).

Otherwise:

$ cd /; a=( $(printf "*\n") )
$ echo ${a[@]}
bin boot cdrom dev etc home ...
added 180 characters in body
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords

The splitting is done with IFS as the delimiter (which contains a space, newline and tab by default). Set the IFS to only the newline:

$ IFS=$'\n' a=($(printf "1 2\n2 3\n"))
$ echo ${a[0]}
1 2
$ echo ${a[1]}
2 3

This will change IFS for the shell, so best save it before and restore it:

OLD_IFS="$IFS"
IFS=$'\n' array=($(grep '^#' threewords | cut -c2-))
IFS="$OLD_IFS"

And there's absolutely no reason to do:

cat threewords | grep '^#'

grep is perfectly capable of reading files:

grep '^#' threewords
Source Link
muru
  • 77.9k
  • 16
  • 212
  • 318
Loading