How To Use The Bash read Command {10 Examples} |
您所在的位置:网站首页 › excel表格删除整行后怎么调整顺序 › How To Use The Bash read Command {10 Examples} |
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. Prerequisites Access to the command line/terminal. Basics of working with environment variables. Bash read SyntaxThe syntax for the Bash read command is: readThe 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 OptionsThe 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 ExamplesThe 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: readThe prompt waits for the user input. 3. Type a sentence and press Enter. The terminal returns to its normal state. 4. Retrieve the message with the echo command: echo $REPLYThe $REPLY variable stores the read command message. Below are examples demonstrating more complex use cases for the Bash read command. ArgumentsSave the user input into a specified variable by providing an argument: read inputRetrieve the message with: echo $inputAlternatively, split the user input into different variables by adding multiple arguments. For example: read var1 var2The user input splits into individual words. Retrieve them with: echo $var1 echo $var2When 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 $var2If there are fewer words than variables, the remaining variables are empty. PipingPiping 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)The parentheses create a subshell with multiple commands, and the individual variables print to the console. HeredocAnother way to input text into the read command is using heredoc notation. For example: read var1 var2 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |