3 ms·
Speaking of redirections... Here's another fut bit of lesser-known shell: f(){ cat;} <file f # prints out contents of file I.e. You can bind redirect
by xelxebar 2y ago
Speaking of redirections... Here's another fut bit of lesser-known shell:
f(){ cat;} <file
f # prints out contents of file
I.e. You can bind redirections to function definitions that end up applying at call time.
- Brian_K_White 2y agowhat is a use of that vs f () { cat file; } Why make the shell open and read the file and pipe in to the cat process when cat would open and read the file itself directly? I guess the point is not cat but the redirection itself. ie, if the contents of f() were not simply cat, and maybe there was no handy place to put the filename within. Though, I think I still struggle to think up an example even then.
- xelxebar 2y agoIn this example? It's clearly useless and somewhat tongue-in-cheek. Redirecting stdout has some straightforward uses, e.g. log(){ foo "$1" bar "$2" } >>logfile Redirecting stdin is a little more subtle: contents(){ maybeReadStdinA maybeReadStdinB } <input where the first command to read stdin gets the contents of input and subsequent commands see nothing.