Shell scripting: read one line at a time from keyboard

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.

Advertisement

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)

🥺 Was this helpful? Please add a comment to show your appreciation or feedback.

nixCrat Tux Pixel Penguin
Hi! 🤠
I'm Vivek Gite, and I write about Linux, macOS, Unix, IT, programming, infosec, and open source. Subscribe to my RSS feed or email newsletter for updates.

1 comment… add one
  • jalal hajigholamali Aug 3, 2011 @ 16:10

    thanks…

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre> for code samples. Your comment will appear only after approval by the site admin.