Facebook Twitter Instagram
    TecAdmin
    • Home
    • Ubuntu 20.04
      • Upgrade Ubuntu
      • Install Java
      • Install Node.js
      • Install Docker
      • Install LAMP Stack
    • Tutorials
      • AWS
      • Shell Scripting
      • Docker
      • Git
      • MongoDB
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    Home»Programming»Bash Shell»Reading File Line by Line in Linux Shell Script

    Reading File Line by Line in Linux Shell Script

    RahulBy RahulNovember 22, 20141 Min Read

    While Loop:

    while read line
    do
    echo $line
    done < /tmp/file.txt

    Note: “line” is a variable which contains a single line read from file.

    Example:-

    Some times we required to read file content line by line inside shell a script. For this example this script will read /etc/passwd file line by line and print usernames with there corresponding home directory.

    #!/bin/bash
    
    while read line
    do
        USERNAME=`echo $line | cut -d":" -f1`
        HOMEDIR=`echo $line | cut -d":" -f6`
        echo "$USERNAME =>  $HOMEDIR"
    done < /etc/passwd
    
    
    file script shell while
    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp
    Previous Article3 Most Popular Tools to Archive Files and Directories in Linux
    Next Article How to Setup Http Proxy for User on Linux

    Related Posts

    How to Backup Website to Amazon S3 using Shell Script

    Updated:March 24, 20222 Mins Read

    How to Search Recently Modified Files in Linux

    2 Mins Read

    Bash Printf Command

    Updated:December 23, 20212 Mins Read

    Creating Menu in Shell Script (Linux Select Command)

    Updated:May 31, 20222 Mins Read

    (Resolved) -bash: /bin/mv: Argument list too long

    Updated:January 13, 20222 Mins Read

    Bash Break and Continue

    Updated:November 9, 20213 Mins Read

    Leave A Reply Cancel Reply

    Recent Posts
    • What is CPU? – Definition, Types and Parts
    • What is the /etc/aliases file
    • What is the /etc/nsswitch.conf file in Linux
    • How to Install Ionic Framework on Ubuntu 22.04
    • What is the /etc/hosts file in Linux
    Facebook Twitter Instagram Pinterest
    © 2022 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.