by Karunamon · 12y ago · 0 comments
One of my main complaints with bash.. the file is evaluated in order - you can't call a function on the line before it's declared.
This fails:
#!/usr/bin/env bash
bar='bar'
foofunction
foofunction(){
echo 'foo'
echo $bar
}
Basically you have to write your entire script in reverse, and i'm unaware of a good way to get around it.You probably want the interpreter to be smart: "Am I loading a script from a file, or am I receiving instructions interactively on the command-line?" But now there's two modes of execution, and code in a bash script won't work if you type it into the prompt yourself. That's a bit uncomfortable.
#! /usr/bin/env bash
set -e -u
bar='bar'
main() {
foofunction
}
foofunction() {
echo 'foo'
echo $bar
}
main "$@"