While playing around with bash and sh, I found out that the following is valid in bash:
system.out.println () { printf "$1"; }
but not in sh:
sh: `system.out.println': not a valid identifier
Why would this difference be there? Does the function defined above violate some convention (POSIX etc.) that causes this error?
bash and sh are two different shells of the Unix operating system. bash is sh, but with more features and better syntax. Bash is “Bourne Again SHell”, and is an improvement of the sh (original Bourne shell). Shell scripting is scripting in any shell, whereas Bash scripting is scripting specifically for Bash.
Bash is largely compatible with sh and incorporates useful features from the Korn shell ksh and the C shell csh. It is intended to be a conformant implementation of the IEEE POSIX Shell and Tools portion of the IEEE POSIX specification (IEEE Standard 1003.1).
bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
You can use $ {FUNCNAME [0]} in bash to get the function name. funcstack in zsh. An array variable containing the names of all shell functions currently in the execution call stack. The element with index 0 is the name of any currently-executing shell function. The bottom-most element (the one with the highest index) is "main".
In bash, function names can be command names, as long as they would be parsed as a WORD without quotes. (Except that, for some reason, they cannot be integers.) However, that is a bash extension.
A bash function is a technique for grouping reusable bits of code under one name for later use. The bash function is like a script within a script. 1. A function is read directly into the shell's memory and stored for later use. Since computer memory is not an issue nowadays, using functions is faster than repeating code.
A command name, on the other hand, can be just about anything which doesn't contain bash metacharacters (and even then, they can be quoted). In bash, function names can be command names, as long as they would be parsed as a WORD without quotes.
It's just the dots, you can't use dots in shell function names. Or any variable name, for that matter.
I'll link you to this question: Allowed characters in linux environment variable names
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With