Read a file line by line assigning the value to a variable

后端 未结 10 1193
说谎
说谎 2020-11-21 07:37

I have the following .txt file:

Marco
Paolo
Antonio

I want to read it line-by-line, and for each

10条回答
  •  悲哀的现实
    2020-11-21 08:23

    I encourage you to use the -r flag for read which stands for:

    -r  Do not treat a backslash character in any special way. Consider each
        backslash to be part of the input line.
    

    I am citing from man 1 read.

    Another thing is to take a filename as an argument.

    Here is updated code:

    #!/usr/bin/bash
    filename="$1"
    while read -r line; do
        name="$line"
        echo "Name read from file - $name"
    done < "$filename"
    

提交回复
热议问题