-3

From bash manual

Any redirections (see Section 3.6 [Redirections], page 31) associated with the shell function are performed when the function is executed.

Where is a "redirection associated with the shell function" specified? Is it specified for a command inside the definition of the function, or for a call to the function?

Why does the quote emphasize that such a redirection is performed when the function is executed?

Thanks.

  • I think it's simply saying that ` foo(){ echo bar; }>file` won't create/truncate a file named file, and only when the function is called will the file be created/truncated. – PSkocik Jul 29 '17 at 23:11
  • do you mean that running foo or foo > file will create/truncate a file named file? – Tim Jul 29 '17 at 23:16
3

In a nutshell the statement in question means, as PSkocik suggested in a comment, that with bar() { ... } > foo you don't have to worry about foo getting overwritten until immediately before your function bar() begins executing. That is, just running a script that defines bar isn't enough to overwrite foo, you actually have to call bar :)

Practically speaking this also means that you can redirect with variables, > "$foo" not > foo because the variable will not be expanded/evaluated until function invocation.

Much discussion about this and other function redirection details can be found here: http://www.linuxjournal.com/content/more-bash-redirections

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.