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
Related
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
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]
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Closed 2 years ago.
I'm using gitlab-ci and i wanna package my artifact with specified VERSION which exported by previous stages, but the key of version value is generated with CI_COMMIT_SHORT_SHA, and i export it like this:
export COMMIT_${CI_COMMIT_SHORT_SHA}_VERSION=${VERSION}
but i can't use it like
echo ${COMMIT_${CI_COMMIT_SHORT_SHA}_VERSION}
or
`echo '$COMMIT_'"${CI_COMMIT_SHORT_SHA}"'_REVISION'`
any suggestions?
Hope below example help you:
$ foo=testvar
$ bar=testvalue
$ declare "someprefix_${foo}=${bar}"
# your variable
$ echo $someprefix_testvar
testvalue
# one way
$ eval echo '$'"someprefix_${foo}"
testvalue
# other way - prints the contents of the real variable
$ out="someprefix_${foo}"
$ echo ${!out}
testvalue
eval : Execute arguments as a shell command.
declare : declare is a bash built-in command that allows you to update attributes applied to variables within the scope of your shell.
Refer : http://mywiki.wooledge.org/BashFAQ/006#Indirection
You need:
$ declare "COMMIT_${CI_COMMIT_SHORT_SHA}_VERSION=${VERSION}"
$ out="COMMIT_${CI_COMMIT_SHORT_SHA}_VERSION"
$ echo ${!out}
Test Results:
[akshay#db1 tmp]$ CI_COMMIT_SHORT_SHA=abcdef
[akshay#db1 tmp]$ VERSION=1.2.0
[akshay#db1 tmp]$ declare "COMMIT_${CI_COMMIT_SHORT_SHA}_VERSION=${VERSION}"
[akshay#db1 tmp]$ out="COMMIT_${CI_COMMIT_SHORT_SHA}_VERSION"
[akshay#db1 tmp]$ echo ${!out}
1.2.0
This question already has answers here:
What's the point of eval/bash -c as opposed to just evaluating a variable?
(3 answers)
The 'eval' command in Bash and its typical uses
(11 answers)
Closed 2 years ago.
There is something i do not understand with strings in bash:
Look at this script:
#!/bin/bash
tmp="ls"
"$tmp"
This script executes ls command and display result in the console.
Now look at this script:
#!/bin/bash
tmp="ls > out.txt"
"$tmp"
This second script does not execute ls and displays this error:
line 3: ls > out.txt: command not found
I just want to understand. I do not want to understand how to run ls command. I want to understand why the first script works and not the second.
Thanks
This question already has answers here:
Use the result of a shell command in a conditional in a makefile
(1 answer)
How to use Bash parameter substitution in a Makefile?
(1 answer)
Closed 4 years ago.
I have next rule in Makefile:
dbrestoretable:
echo ${TABLE:-asdf}
When I run:
$ make dbrestoretable
echo
But:
$ echo ${TABLE:-asdf}
asdf
Why default value asdf is not echoed in first case?
UPD
Specified duplicate speaks about how to use variable assigned in Makefile. And it does not answer why default value is not used
Look at this modified example:
dbrestoretable:
echo ${TABLE:-asdf}
echo ${TABLE}
Then run:
$ make dbrestoretable TABLE=mytable
echo
echo mytable
$ make dbrestoretable
echo
echo
As you can see when I use ${TABLE:-asdf} it just return empty string. But I expect in first run mytable and second run asdf value to be echoed