(Otherwise we'd need to remember that we've switched things around--something that might be impractical for complex scripts. Reading sort's output into an array variable: Bash v4+: readarray -t a_out reads the individual lines output by sort into the elements of array variable a_out, without including the trailing \n in each element (-t). Fortes About Merging piped stdin with command-line arguments within Bash. As a result, the loop is executed once for every successive zero-terminated array element, with the value being stored in e. The example just puts the items in another array but you may prefer to process them directly :). This is extracted from the main bash man page, see there for more details. This resets the value of IFS to the default value, and is just good practice. Nice, it should be also noted that readarray is available since version 4 of bash. I would almost ask for a demo on how ksh would use the set -s flag... but then again, the question is on bash, so that would be rather off-topic, You are a veritable fountain of shell knowledge. An old boat in Corcovado National Park Osa, Costa Rica. Have there been any instances where both of a state's Senate seats flipped to the opposing party in a single election? How do you run a test suite from VS Code? ie. Redirecting multiple commands to the same file, Using "trap" to react to signals and system events. Interesting technique, I've used a variant for finding max/min values without explicit compare/sort. Before going through this article, let us understand what the terms stdin, stdout and stderr are.. Standard input – This is the file-handle that a user program reads to get information from the user. I remember quick sort from college days but will also research bubble sort. (This is a bash feature, not Linux feature). Could you explain for the average bash user how this solution works? Take this for example: head < filename.txt These functions are pure Bash! cmd <(cmd1) Redirect stdout of cmd1 to an anonymous fo, then pass the fo to cmd as an argument. There are several options for the readarray command. After that, … Note: as @sorontar mentioned in a comment to a different question: To subscribe to this RSS feed, copy and paste this URL into your RSS reader. Is it possible to make a video that is provably non-manipulated? It's common in Linux to pipe a series of simple, single purpose commands together to create a larger solution tailored to our exact needs. Read the stream into an array with mapfile (e.g. IFS=$'\n' read -d '' -r -a a_out tells read to read into array (-a) variable a_out, reading the entire input, across lines (-d ''), but splitting it into array elements by newlines (IFS=$'\n'. Unfortunately, as correctly signaled by @ruakh this didn't mean the the result of readarray would be correct, because readarray has no option to use NUL instead of regular newlines as line-separators. How can I pair socks from a pile efficiently? The -t option will remove the trailing newlines from each line. You should turn glob off: set -f or set -o noglob or shopt -op noglob or an element of the array like * will be expanded to a list of files. However, bash (unlike ksh) spawns a subshell for $(