Tutorial by Examples

#!/bin/bash FILENAME="/etc/passwd" while IFS=: read -r username password userid groupid comment homedir cmdshell do echo "$username, $userid, $comment $homedir" done < $FILENAME In unix password file, user information is stored line by line, each line consisting of i...
readarray -t arr <file Or with a loop: arr=() while IFS= read -r line; do arr+=("$line") done <file
while IFS= read -r line; do echo "$line" done <file If file may not include a newline at the end, then: while IFS= read -r line || [ -n "$line" ]; do echo "$line" done <file
var='line 1 line 2 line3' readarray -t arr <<< "$var" or with a loop: arr=() while IFS= read -r line; do arr+=("$line") done <<< "$var"
var='line 1 line 2 line3' while IFS= read -r line; do echo "-$line-" done <<< "$var" or readarray -t arr <<< "$var" for i in "${arr[@]}";do echo "-$i-" done
while IFS= read -r line;do echo "**$line**" done < <(ping google.com) or with a pipe: ping google.com | while IFS= read -r line;do echo "**$line**" done
Let's assume that the field separator is : (colon) in the file file. while IFS= read -d : -r field || [ -n "$field" ]; do echo "$field" done <file For a content: first : se con d: Thi rd: Fourth The output is: **first ** ** se con d** ** Thi ...
Let's assume that the field separator is : var='line: 1 line: 2 line3' while IFS= read -d : -r field || [ -n "$field" ]; do echo "-$field-" done <<< "$var" Output: -line- - 1 line- - 2 line3 -
Let's assume that the field separator is : arr=() while IFS= read -d : -r field || [ -n "$field" ]; do arr+=("$field") done <file
Let's assume that the field separator is : var='1:2:3:4: newline' arr=() while IFS= read -d : -r field || [ -n "$field" ]; do arr+=("$field") done <<< "$var" echo "${arr[4]}" Output: newline
Let's assume that the field separator is : while IFS= read -d : -r field || [ -n "$field" ];do echo "**$field**" done < <(ping google.com) Or with a pipe: ping google.com | while IFS= read -d : -r field || [ -n "$field" ];do echo "**$field**&q...

Page 1 of 1