running several comands in a bash script [duplicate] - bash

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

Related

Bash outputs p:line 5: modavar: command not found when i aliased modaver [duplicate]

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

Calling and passing variables values between shell scripts [duplicate]

This question already has answers here:
Pass all variables from one shell script to another?
(7 answers)
Closed 3 years ago.
OS: Ubuntu 18.04
Bash
I am trying to get one shell script to pass variables to another script and execute it. Here's what I tried:
mainscript.sh
#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")
subscript1.sh:
#!/bin/bash
# subscript1.sh
echo $test_string
But, when I do:
bash mainscript.sh
I get nothing. Any ideas on how to do this?
Shell variables are by default not visible in child processes. To pass them to children use the export keyword:
#!/bin/bash
# Master script
SUBSCRIPT1_PATH=~/subscript1.sh
export test_string="The cat ate the canary"
(exec "$SUBSCRIPT1_PATH")

An alias defined in a bash script does not work within the same script [duplicate]

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

The function definitions in shell use dash [duplicate]

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.

Alias doesn't work inside a Bash script [duplicate]

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

Resources