How To Use The Bash read Command {10 Examples}

您所在的位置:网站首页 excel表格删除整行后怎么调整顺序 How To Use The Bash read Command {10 Examples}

How To Use The Bash read Command {10 Examples}

2024-01-15 05:12| 来源: 网络整理| 查看: 265

Introduction

The Bash read command is a built-in utility that reads text from standard input. The tool offers many functionalities for reading user input, helping make Bash scripts interactive.

This guide explains how the Bash read command works through various examples and use cases.

How to Use the Bash read Command

Prerequisites

Access to the command line/terminal. Basics of working with environment variables. Bash read Syntax

The syntax for the Bash read command is:

read

The read command takes the user input and splits the string into fields, assigning each new word to an argument. If there are fewer variables than words, read stores the remaining terms into the final variable.

Specifying the argument names is optional. The command stores a user's input into the $REPLY variable by default.

Bash read Options

The Bash read command comes with many options to control the user input. Some options do not require additional parameters, while others have mandatory parameters.

The table below shows all the possible command options and their description.

OptionDescription-a Assigns the provided word sequence to a variable named .-d Reads a line until the provided instead of a new line.-eStarts an interactive shell session to obtain the line to read.-i Adds initial text before reading a line as a prefix.-n Returns after reading the specified number of characters while honoring the delimiter to terminate early.-N Returns after reading the specified number of chars, ignoring the delimiter.-p Outputs the prompt string before reading user input.-rDisable backslashes to escape characters.-sDoes not echo the user's input.-t The command times out after the specified time in seconds.-u Read from file descriptor instead of standard input.

Continue reading to see how the read command works through various examples.

Bash read Examples

The read command functions without any arguments or options. To test the command, follow the steps below:

1. Open the terminal.

2. Write the command and press Enter:

read read terminal output

The prompt waits for the user input.

3. Type a sentence and press Enter.

read command with sentence terminal output

The terminal returns to its normal state.

4. Retrieve the message with the echo command:

echo $REPLY echo reply variable terminal

The $REPLY variable stores the read command message.

Below are examples demonstrating more complex use cases for the Bash read command.

Arguments

Save the user input into a specified variable by providing an argument:

read input

Retrieve the message with:

echo $input read into variable and echo terminal output

Alternatively, split the user input into different variables by adding multiple arguments.

For example:

read var1 var2

The user input splits into individual words. Retrieve them with:

echo $var1 echo $var2 read into two variables and echo terminal output

When the user input has more words than there are variables, the extra words are assigned to the last provided variable:

read var1 var2 foo bar baz echo $var1 echo $var2 read variables and word count terminal

If there are fewer words than variables, the remaining variables are empty.

Piping

Piping takes the standard output from one command and parses it as standard input for another process. Use echo and pipe the information to read for immediate parsing. For example:

echo "Hello world!" | (read var1 var2; echo $var1; echo $var2) piping echo and read

The parentheses create a subshell with multiple commands, and the individual variables print to the console.

Heredoc

Another way to input text into the read command is using heredoc notation. For example:

read var1 var2


【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3