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
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:
Alias doesn't work inside a Bash script [duplicate]
(2 answers)
Closed 4 years ago.
I have some commands I would like to run in the following order:
HEADAS=/home/warano/HEASoft/heasoft-6.24/x86_64-pc-linux-gnu-libc2.27
export HEADAS
alias heainit=". $HEADAS/headas-init.sh"
heainit
CALDB=/home/warano/NUSTAR/caldb
export CALDB
source $CALDB/software/tools/caldbinit.sh
I put all of these in a script called run-nu_tools.sh, but it does not work so I got this output:
./run-nu_tools.sh: line 6: heainit: command not found
However heainit works if one runs everything in the terminal(step by step) so I want to run all at once, do you have any tips?
The problem is that aliases are not expanded if the shell is not interactive:
ALIASES: Aliases are not expanded when the shell is not interactive unless the expand_aliases shell option is set using shopt.
source: man bash
Add the following to your script:
shopt -s expand_aliases
This question already has answers here:
How do I get the directory where a Bash script is located from within the script itself?
(74 answers)
Closed 4 years ago.
I have a script, that configures some environment variables. Therefore it needs its own position, the value of $0.
I have another script that sources the first script, but it could also happen that the first script is sourced from the console.
However, neither $0, nor ${BASH_SOURCE[0]} seem to work consistently on MacOS. Here is a small example to show what I mean:
a.sh:
#!/bin/bash -eu
source ./b.sh
b.sh:
#!/bin/bash -eu
echo "$0"
echo "${BASH_SOURCE[0]}"
Output of ./a.sh:
./a.sh
./b.sh
Output of source ./b.sh:
./b.sh
<empty line, can't get stackoverflow to display it properly>
Not relevant, but just for completeness: output of ./b.sh:
./b.sh
./b.sh
So, in short: If I use $0 to get the script name, it is wrong when sourced via another script. If I use ${BASH_SOURCE[0]}, it will be empty when sourced directly.
How can I get the script name in both cases?
I just found out that ${BASH_SOURCE[0]:-$0} resolves to the correct script name in both cases.
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
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.