Skip to main content
fixed formatting/ -> syntax highlighting
Link
AdminBee
  • 23.6k
  • 25
  • 55
  • 77

I found some code for reading input from a file a while ago, I believe from Stack Exchange, that I was able to adapt for my needs:

while read -r line || [[ -n "$line" ]]; do
    if [[ $line != "" ]]
    then
        ((x++));
        echo "$x:  $line"
    <then do something with $line>
    fi
done < "$1"
while read -r line || [[ -n "$line" ]]; do
    if [[ $line != "" ]]
    then
        ((x++))
        echo "$x: $line"
    # <then do something with $line>
    fi
done < "$1"

I'm reviewing my script now & trying to understand what it's doing ... II don't understand what this statement is doing:

while read -r line || [[ -n "$line" ]];

while read -r line || [[ -n "$line" ]];

I understand that the -r-r option says that we're reading raw text into line$line, but I'm confused about the || [[ -n "$line" ]] portion of the statement. CanCan someone please explain what that is doing?

I found some code for reading input from a file a while ago, I believe from Stack Exchange, that I was able to adapt for my needs:

while read -r line || [[ -n "$line" ]]; do
    if [[ $line != "" ]]
    then
        ((x++));
        echo "$x:  $line"
    <then do something with $line>
    fi
done < "$1"

I'm reviewing my script now & trying to understand what it's doing ... I don't understand what this statement is doing:

while read -r line || [[ -n "$line" ]];

I understand that the -r option says that we're reading raw text into line, but I'm confused about the || [[ -n "$line" ]] portion of the statement. Can someone please explain what that is doing?

I found some code for reading input from a file a while ago, I believe from Stack Exchange, that I was able to adapt for my needs:

while read -r line || [[ -n "$line" ]]; do
    if [[ $line != "" ]]
    then
        ((x++))
        echo "$x: $line"
    # <then do something with $line>
    fi
done < "$1"

I'm reviewing my script now & trying to understand what it's doing ...I don't understand what this statement is doing:

while read -r line || [[ -n "$line" ]];

I understand that the -r option says that we're reading raw text into $line, but I'm confused about the || [[ -n "$line" ]] portion of the statement. Can someone please explain what that is doing?

edited title
Link
muru
  • 77.9k
  • 16
  • 212
  • 317

Explanation for what a while What does `while read statement is doing-r line || [[ -n $line ]]` mean?

Source Link
Loading