To read a line field by field in Bash, you can use the read
command, which allows you to read individual fields from a line of input and store them in separate variables.
For example, suppose you have a line of input that contains three fields, separated by commas, like this:
field1,field2,field3
To read this line and store each field in a separate variable, you can use the read
command like this:
$ read var1 var2 var3 <<< "field1,field2,field3"
This will read the line of input and store each field in a separate variable. The first field will be stored in the var1
variable, the second field will be stored in the var2
variable, and the third field will be stored in the var3
variable.
If you want to specify a different field separator for the read
command to use, you can use the -d
option, followed by the field separator character or string. For example, to read the line of input using a semicolon (;
) as the field separator, you can use the following command:
$ read -d ";" var1 var2 var3 <<< "field1;field2;field3"
This will read the line of input using the semicolon as the field separator, and store each field in a separate variable.
Overall, the read
command is a simple and effective way to read a line field by field in Bash. By using this command, you can easily and quickly read individual fields from a line of input, allowing you to manipulate and process the data in the fields in a variety of ways.