This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 6 years ago.
The following code:
#!/bin/bash
function me-test()
{
echo 'test'
}
me-test
The execution method below is not correct:
#sh 1.sh
1.sh: line 6: `me-test': not a valid identifier
but the execution method below is correct:
#./1.sh
test
In other programming languages,it can not using dash to define function.For example,python.
why shell is so that?
sh 1.sh runs the script as a Bourne Shell script and ./1.sh runs it as a bash script because of your line #!/bin/bash (this is called a shebang which tells the shell which interpreter to use) calls the bash interpreter to run the script. bash allows for hyphens in function names but Bourne Shell does not. The two are very similar but different programming languages.
If you changed #!/bin/bash to #!/bin/sh you'd get an error every time you ran the program.
Related
This question already has answers here:
How to set an alias inside a bash shell script so that is it visible from the outside? [duplicate]
(4 answers)
Closed 2 years ago.
I have an executable file command.sh
#/bin/bash
alias my_command='echo ok'
my_command
My terminal is bash.
When I run it like ./command.sh, it works fine.
When I run it like /bin/bash ./command.sh, it can't find a my_command executable.
When I run it like /bin/sh ./command.sh, it works fine.
I'm confused here. Where's the problem?
From the bash man page:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt (see the
description of shopt under SHELL BUILTIN COMMANDS below).
In other words, aliases are not enabled in bash shell scripts by default. When you run the script with bash, it fails.
Your sh appears to default to allowing aliases in scripts. When you run the script with sh, it succeeds.
./command.sh happens to work because your shebang is malformed (you're missing the ! in #!/bin/bash).
Aliases are for the interactive shell, what you want here is a function, e.g.
#!/bin/bash
function my_command() {
echo ok
}
my_command
This question already has answers here:
Bash script process substitution Syntax error: "(" unexpected
(3 answers)
shebang not working to run bash scripts in linux
(1 answer)
Syntax error: "(" unexpected -- with !(*.sh) in bash script [duplicate]
(3 answers)
syntax error near unexpected token `<'
(2 answers)
Closed 3 years ago.
I have a .sh file. It includes bash syntax.
#!/bin/bash
function foo() {
// do something
}
doo()
// do something
sh doesn't link to bash on my system.
Below command doesn't work:
sh sample.sh
It throws syntax error. Below command works fine.
bash sample.sh
I think '#!/bin/bash' is useless for my case. I know that sh != bash. But do I must specify sh/bash/etc like upper example to run .sh file?
The shebang (#!) is only used if you execute the file (i.e: ./sample.sh). Of course, the file must have execution permissions.
If you execute sh with your file as argument its content is interpreted by sh (not taking into account the shebang). The same for bash or any other command that reads the file represented by the first argument.
This question already has answers here:
How to read a file into a variable in shell?
(9 answers)
Difference between sh and Bash
(11 answers)
Closed 4 years ago.
I have store ip address with port in a file and I want to read it using shell script. Thus file serverIP has data 192.168.1.17:3000. I am using following bash script to read it
IPAddressFile=/home/geo/serverIP
SERVER_IP_PORT=$(<$IPAddressFile)
echo $SERVER_IP_PORT
But this script echo empty string. Where I am making mistake?
If you're going to use bash-only syntax like $(<...), your script must be run with bash, not sh.
Thus, either run bash yourscript or add a #!/bin/bash (or similar) shebang, flag the file executable, and invoke it as a command, for example ./yourscript
As an alternative that's both efficient and compatible with POSIX sh:
IFS= read -r SERVER_IP_PORT <"$IPAddressFile"
This question already has answers here:
Writing a Bash script without the shebang line
(2 answers)
Bash script execution with and without shebang in Linux and BSD
(2 answers)
Closed 5 years ago.
If after logging into my system I type: bash (to use bash subshell) and then try to run a bash script (e.g. example.sh), then does it matter if I do not put #!/bin/bash as the first line of the script or it is fine since I am already inside bash subshell?
This question already has answers here:
How do create an alias in shell scripts? [duplicate]
(3 answers)
Closed 5 years ago.
I have a simple bash script.
alias myls=ls
myls
If I execute this script, I get an error.
$ bash foo.sh
foo.sh: line 2: myls: command not found
Why does the alias not work in the script?
Does this behavior conform to POSIX?
If it is indeed not supposed to work, could you please point me to an authoritative documentation that stays this?
See man bash:
Aliases are not expanded when the shell is not interactive, unless the expand_aliases shell option is set using shopt