Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion re: expr deprecation for strings #41

Closed
apennebaker opened this issue Nov 20, 2013 · 6 comments
Closed

Confusion re: expr deprecation for strings #41

apennebaker opened this issue Nov 20, 2013 · 6 comments

Comments

@apennebaker
Copy link

Shellcheck says to replace expr ..., as expr is deprecated. For mathematical expressions, I can use $((...)) instead. But what should I use for string expressions?

FizzBuzz example:

mcandre/toys#2 (comment)

@koalaman
Copy link
Owner

Is this just to write the value? Use echo for that.

@apennebaker
Copy link
Author

I want to write a shell function that returns a string, without printing it (very functional programming style). But it's not easy, as shell functions normally only return exit codes.

For FizzBuzz, I gave up, and had the fizz function go ahead and print the value instead of returning it. My Lisp senses are tingling, but whatcha gonna do...

@koalaman
Copy link
Owner

Writing to stdout is how you return data from functions. The caller decides what happens to it, whether it's captured or merged with the caller's stdout. It's perfectly functional.

@apennebaker
Copy link
Author

Interesting. I suppose there are ways to redirect the output before/when the function is executed, so that the user doesn't see the string in the real STDOUT?

@koalaman
Copy link
Owner

Yes. you can capture/expand the data with variable=$(somefunction), pipe it with somefunction | otherfunction, redirect it with somefunction > somefile, and there are process substitutions and coprocs, and they can all be combined and nested arbitrarily.

In lisp/scheme, (+ 2 3) will print 5 to the screen if you run it from a repl, but not if you use it as a sub-expression unless the function implicitly or explicitly returns it unmodified. That's the same thing.

There's no "real stdout" in bash, just like there's no "real return value" in lisp. There's just your own return value/stdout, and what the caller did with it.

@apennebaker
Copy link
Author

Pipe |! Genius!

gerow pushed a commit to gerow/shellcheck that referenced this issue Jul 29, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants