Default variable in bash when using -u flag? [duplicate] - bash

This question already has answers here:
How to write a bash script that takes optional input arguments?
(9 answers)
Closed 4 months ago.
It is not hard to set a default variable in bash. E.g: How to write a bash script that takes optional input arguments?
But, these methods fail when using set -euxo pipefail. What methods are compatible with the -u flag?
Example:
set -euxo pipefail
foo=${2:test}
echo $foo
for Bash GNU bash, version 5.0.17(1)-release (x86_64-pc-linux-gnu)

The accepted answer to the linked duplicate works fine -- you're just misreading it.
It suggests:
somecommand ${1:-foo} # GOOD
...or...
somecommand ${1-foo} # GOOD
...not...
somecommand ${1:foo} # BAD

Related

Printing arrays are not working in shell script [duplicate]

This question already has answers here:
Why does my Bash code fail when I run it with 'sh'?
(2 answers)
Closed 10 months ago.
I am kind of new in shell scripting and trying to learn arrays. I declared array value but when I am trying to print that array it is giving me an error(bad substitution).
I am pasting the code below, please suggest to me what is wrong here-
➜ ~ cat test.sh
#!/bin/bash
array=['foo','bar','a','b']
echo 1
echo "${array[0]}"
➜ ~ sh test.sh
1
test.sh: 5: Bad substitution
Thanks in advance.
Depending on the system you're using sh might be not be Bash and
it's not Bash on yours, it can be dash for example. Run your script with Bash:
$ bash arr.sh
1
[foo,bar,a,b]
Or set an executable bit and call the script without providing the name of the interpreter since you already have the shebang:
$ chmod +x test.sh
$ ./test.sh
1
[foo,bar,a,b]

Operator in variable affectation [duplicate]

This question already has answers here:
What is the difference between "var=${var:-word}" and "var=${var:=word}"?
(4 answers)
Closed 1 year ago.
I'm trying to understand the syntax of an existing ksh script. I came across the following line:
HOME_APP=${HOME_APP:-/app}
What does it mean?
Seems like there must be a duplicate for this, but :- is used to supply a default value for the expansion when HOME_APP is unset or null.
$ unset HOME_APP
$ echo "${HOME_APP:-/app}"
/app
$ HOME_APP=
$ echo "${HOME_APP:-/app}"
/app
$ HOME_APP=/opt
$ echo "${HOME_APP:-/app}"
/opt

work when excute in shell but not work from bash script file [duplicate]

This question already has answers here:
Difference between sh and Bash
(11 answers)
Closed 2 years ago.
a.sh content
#!/bin/bash
awk '{u=$2+$4; t=$2+$4+$5; if (NR==1){u1=u; t1=t;} else print ($2+$4-u1) * 100 / (t-t1) "%"; }' <(grep 'cpu ' /proc/stat) <(sleep 5;grep 'cpu ' /proc/stat)
It works when pasted to shell directly,but not work when executed as a script file sh a.sh.
what is the difference ?
This most likely mean your default shell is not "/bin/bash". You can see which shell is your default shell with this command:
echo $SHELL
If you never changed it, chances are that it is "/bin/sh".
Since it works in your shell, the easiest is to change your shebang line at the top so that it reference the path of your default shell instead of "/bin/bash".
But if you end up wanting to make it work in bash and change your default shell to Bash, this is how you do it:
chsh -s /bin/bash

How to send BASH variables to multiple scripts? [duplicate]

This question already has answers here:
Pass all variables from one shell script to another?
(7 answers)
Closed 8 years ago.
I have many BASH scripts called in sequence, e.g., script1.sh contains:
#!/bin/bash
bash script2.sh
bash script3.sh
bash script4.sh
script2.sh contains:
#!/bin/bash
file_a="1.txt"
cp $file_a /tmp/$file_a.tmp
script3.sh contains:
#!/bin/bash
wc -l /tmp/$file_a.tmp
script4.sh contains:
#!/bin/bash
cat /tmp/2.txt $file_a.tmp > file3.txt
Each file requires access to a small collection of variables. How can I pass the variables from one script onto the next?
You have many options.
The first method would be making the variable as the environment variable and pass to the script before the second script get executed.
The second method would be making the second script to run in the same shell.
The methods are described well here with the examples.

bash script and command definition [duplicate]

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Closed 3 years ago.
Got a little bash script like so:
#!/bin/bash
TIME_CMD='/usr/bin/time -f "%E execution time"'
${TIME_CMD} ls
Only problem: doesn't work:
/usr/bin/time: cannot run execution: No such file or directory
Command exited with non-zero status 127
"0:00.00
What am I doing wrong?
Try making it...
#!/bin/bash
TIME_CMD='/usr/bin/time -f "%E execution time"'
eval "$TIME_CMD ls"
This will utilize bash to re-parse the command string after it has been constructed, so that the quoted argument will be recognized properly.
Storing commands in variables is generally a bad idea (see BashFAQ #050 for details). The reason it's not working as you expect is that quoting inside variable values is ignored (unless you run it through something like eval, which then tends to lead to other parsing oddities).
In your case, I see three fairly straightforward ways to do it. First, you can use an alias instead of a variable:
alias TIME_CMD='/usr/bin/time -f "%E execution time"'
TIME_CMD ls
Second, you can use a function:
TIME_CMD() { /usr/bin/time -f "%E execution time" "$#"; }
TIME_CMD ls
Third, you can use an array rather than a simple variable:
TIME_CMD=(/usr/bin/time -f "%E execution time")
"${TIME_CMD[#]}" ls
Note that with an array, you need to expand it with the "${array[#]}" idiom to preserve word breaks properly.

Resources