User Tools

Site Tools


bash:files:read_a_file:input_source_selection

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

bash:files:read_a_file:input_source_selection [2021/01/26 13:57] – created peterbash:files:read_a_file:input_source_selection [2021/01/26 13:59] (current) peter
Line 1: Line 1:
 ====== BASH - Files - Read a file - Input source selection ====== ====== BASH - Files - Read a file - Input source selection ======
 +
 +Usually read has a filename as its input.
 +
 +To instead use the contents of a variable/parameter as the file to read:
 +
 +<code bash>
 +while IFS= read -r line; do
 +  printf '%s\n' "$line"
 +done <<< "$var"
 +</code>
 +
 +----
 +
 +The same can be done in any Bourne-type shell by using a "here document" (although read -r is POSIX, not Bourne):
 +
 +<code bash>
 +while IFS= read -r line; do
 +  printf '%s\n' "$line"
 +done <<EOF
 +$var
 +EOF
 +</code>
 +
 +----
  
bash/files/read_a_file/input_source_selection.1611669462.txt.gz · Last modified: 2021/01/26 13:57 by peter

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki