Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Function name valid in bash but not in sh [duplicate]

Tags:

bash

shell

sh

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?

like image 700
user2064000 Avatar asked Dec 25 '13 10:12

user2064000


People also ask

What is the difference between the bash and sh shells?

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.

Is bash compatible with SH?

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).

What is $@ in bash?

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.

What is ${} in shell script?

$() – the command substitution. ${} – the parameter substitution/variable expansion.

How to get the name of a shell function in Bash?

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".

Can a function name be a command name?

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.

What is a Bash function?

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.

What is the difference between Bash and a command name?

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.


1 Answers

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

like image 56
Noctua Avatar answered Oct 26 '22 01:10

Noctua