Issue
What does this shell-script line ( IFS=':' read -r -a array <<< "$passPercent" )
within parenthesis is doing.
Please also explain what each parameter is used for in this line .
$passpercent is actually a variable which is storing some curl data in it.
Solution
read -a array
reads and splits the text from standard input into the variable array
. The splitting happens on the value of IFS
which is set to colon for the duration of this operation.
The -r
option to read
disables some legacy behavior with backslashes, and should basically always be used unless you are trying to emulate how the shell itself handles backslashes.
The "here string" <<<value
passes value
as standard input to the process.
Parentheses cause the command to be run in a subshell. It doesn't particularly make sense here; often, we use a subshell to limit the scope of a variable assignment (overriding IFS
locally perhaps; but here, IFS
is only assigned for the duration of read
anyway) or change of working directory.
Perhaps I should specifically point out that
variable=value command
sets the value of variable
just for the duration of the execution of command
, which is quite different from
variable=value; command
which sets the value of variable
from here on, and then executes command
and any subsequent code with the variable set to this value until it gets reassigned again, or this shell instance terminates.
So, in essence, you are creating a new shell instance, setting an array variable by splitting the input variable on colons, and then immediately losing that value when this subshell exits. If you just wanted nothing to happen, :
would be briefer, sleep
would take more time and waste less CPU, and performing some arithmetic in a tight loop would consume more CPU. And if you wanted the result of the assignment to be useful, don't perform it in a subshell (i.e. take out the parentheses).
For what it's worth, the "here string" redirection is a Bash feature (though it is slated to be included in a future version of POSIX sh
) and, of course, so are arrays.
Answered By - tripleee Answer Checked By - Gilberto Lyons (WPSolving Admin)