Function return string in a shell script - Programming On Unix
Users browsing this thread: 1 Guest(s)
|
|||
Yes, it works like that, $(...) captures everything that was written to STDOUT.
Now I guess that the "... to STDOUT"-part is news to you. Usually, three channels are associated with each process:
These channels are not as special as you might think. They're just normal file descriptors. When a process opens another file, it gets file descriptor 3 and so on. When you use a pipe, you connect STDOUT of the first program with STDIN of the second one: "ls -al | grep foo". You can choose the desired channel in your script, like this: Code: #! /bin/sh You'll see that the first two strings get written to the terminal directly (because they're written to STDERR), whereas the last string is written to STDOUT and thus ends up as your "return value". Look up "standard streams" and "shell redirection" to learn more. (Kind of a philosophical issue: Usually, a tool wouldn't print anything unless there's something important to say. There should be a verbose switch "-v", which the user can use to find out exactly what's going on.) |
|||
Messages In This Thread |
Function return string in a shell script - by Tristelune - 07-08-2015, 01:22 PM
RE: Function return string in a shell script - by movq - 08-08-2015, 05:06 AM
RE: Function return string in a shell script - by Tristelune - 09-08-2015, 02:52 PM
RE: Function return string in a shell script - by xero - 10-08-2015, 02:57 PM
RE: Function return string in a shell script - by venam - 01-09-2020, 06:04 AM
|