Shell scripting: read one line at a time from keyboard

last updated in Categories

Sometime it is necessary to read one line from a file or keyboard. We have Shell script utility to read a file line by line here and here. However, you can use line command to do same thing.

For example, read one line from keyboard and store to variable called na:

$ na=$(line)

Howdy Guest

$ echo $na

Output:

Howdy Guest

The utility line copies one line (up to a newline) from standard input (keyboard) to standard output (screen). It always prints at least a newline and returns an exit status of 1 on EOF or read error.

You can easily use line command in a shell scripts.

(Check out all of our posts on Shell Scripting)

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 1 comments/add one below):

1 comment

    Have a question? Post it on our forum!