Skip to main content
added 26 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string happens to beis divided into lines by the inclusion of newline characters in the string has no specialparticular significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that does not matter other thanthis is done only for presentation purposes.

To output only a single line, or any other substring, from the poem, from in the second array element, we would need to use some utility to parse the string. This is Extracting individual newline-delimited substrings from a string does not something that is included inhave anything to do with the concept of arrays in the shell.

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline characters in the string has no special significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that does not matter other than for presentation purposes.

To output only a single line, or any other substring, from the poem, from the second array element, we would need to use some utility to parse the string. This is not something that is included in the concept of arrays in the shell.

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string is divided into lines by the inclusion of newline characters in the string has no particular significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but this is done only for presentation purposes.

To output only a single line, or any other substring, from the poem in the second array element, we need to use some utility to parse the string. Extracting individual newline-delimited substrings from a string does not have anything to do with the concept of arrays in the shell.

added 237 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline characters in the string has no special significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that does not matter other than for presentation purposes.

To output only a single line, or any other substring, from the poem, from the second array element, we would need to use some utility to parse the string. This is not something that is included in the concept of arrays in the shell.

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline characters in the string has no special significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that does not matter other than for presentation purposes.

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline characters in the string has no special significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that does not matter other than for presentation purposes.

To output only a single line, or any other substring, from the poem, from the second array element, we would need to use some utility to parse the string. This is not something that is included in the concept of arrays in the shell.

deleted 26 characters in body
Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string,string; the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline-characters characters in the string has no special significance to the storage of the string. ItThe shell can not be indexed by the shellindex it on lines, and a single line can't contain an embedded newline character without encoding it in some waysomehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any sort of standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). An However, an array element can't however be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky byby Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that really does not matter other than for presentation purposes.

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string, the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline-characters in the string has no special significance to the storage of the string. It can not be indexed by the shell on lines and a single line can't contain an embedded newline character without encoding it in some way.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any sort of standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). An array element can't however be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines, but that really does not matter other than for presentation purposes.

A string of newline-delimited substrings is not the same thing as an array of strings. One is a string; the other contains strings.

The fact that your string happens to be divided into lines by the inclusion of newline characters in the string has no special significance to the storage of the string. The shell can not index it on lines, and a single line can't contain an embedded newline character without encoding it somehow.

The array is an ordered set of separate strings. Each string is immediately accessible via an index into the array. A single array element may contain any standard string, with or without newlines or other delimiting characters (except the nul character in the bash shell). However, an array element can't be another array, as bash does not support multi-dimensional arrays.

string1='Hello World' string2="'Twas brillig, and the slithy toves Did gyre and gimble in the wabe: All mimsy were the borogoves, And the mome raths outgrabe." array=( "$string1" "$string2" ) printf '%s\n' "${array[1]}" 

The above script fragment prints the first verse from the poem Jabberwocky by Lewis Carroll. It does not print Hello World as we choose to output the array's second element, not the first. The second element is a single string made up of characters. Some of those characters happen to be newlines and blanks, but that does not matter other than for presentation purposes.

Source Link
Kusalananda
  • 356.1k
  • 42
  • 737
  • 1.1k
Loading