Question:
In powershell $_
is the name of the current variable being passed with pipes. What is the equivalent of this in Bash?
Let’s say I want to do this
1 2 3 |
echo "Hi" | echo "$_" prints Hi |
Thanks
Answer:
Bash (or any other Unix shell for that matter) has no such thing.
In PowerShell, what is passed through pipes is an object. In bash, this is the output (to stdout) of the command.
The closes thing you can do is use while
:
1 2 |
thecmd | while read theline; do something_with "$theline"; done |
Note that
IFS
(Input Field Separator) is used, you can therefore also do:
1 2 |
thecmd | while read first therest; do ...; done |