This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 6 years ago.
It is doing something to my path_bash variable but what?
Google pulls up this but I can't find it exactly.
#!/bin/bash
path_bash="$HOME/root/config/bash/"
source "${path_bash}_private.sh"
source "${path_bash}config.sh"
source "${path_bash}utility.sh"
source "${path_bash}workflow.sh"
source "${path_bash}net.sh"
source "${path_bash}makeHTM.sh"
and can I put
path_bash
in another file?
It's used to tell bash where the name of your variable ends.
And example to explain:
$ a="gg"
$ echo $ab
$ echo ${a}b
ggb
Related
This question already has answers here:
Difference between ${} and $() in Bash [duplicate]
(3 answers)
Brackets ${}, $(), $[] difference and usage in bash
(1 answer)
Closed 6 months ago.
This post was edited and submitted for review 6 months ago and failed to reopen the post:
Original close reason(s) were not resolved
In the bash shell,
$ date +%Y%m%d%H%M
202208161535
So I put it in a bash script
#!/bin/bash
dat=${date +%Y%m%d%H%M}
echo "dat=" ${dat}
But when I run it, I get
$ test.bash
./test.bash: line 2: ${date +%Y%m%d%H%M}: bad substitution
dat=
How should I do it?
ADD
I found
dat=`date +%Y%m%d%H%M`
works. But I'm curious how I can do it with dat = ${data +%Y%m%d%H%M}.
ADD2
This question arose because of the mistake, or rather from not knowing the difference of ( ) and { }. Those who cannot notice this difference cannot search with search pattern 'difference of ( ) and { } in bash'. So the referenced links 'supposed to have solution for this question' cannot be searchable by the people like me. So I think this question is worth being kept as is.
#!/bin/bash
dat=`date +%Y%m%d%H%M`
echo "dat="${dat}
The above code is working code
This question already has answers here:
Dynamic variable names in Bash
(19 answers)
How can I generate new variable names on the fly in a shell script?
(6 answers)
Closed 12 months ago.
So, I'd like to do something like the following:
while [ $n -le 7 ]
do
char$1=somevalue
echo $char$1
n++
done
But even reading through the links below and trying declare or something like $char${!n}, I haven't been able to make it work... Halp! :)
I've already looked at:
How to use a variable's value as another variable's name in bash
and
Dynamic variable names in Bash
Thanks!
This question already has answers here:
Reading java .properties file from bash
(13 answers)
Closed 4 years ago.
I have a property s3Path=s3a://myBucket in a file s3-apps.properties
Currently the path to a S3 is hardcoded in my bash script:
thePath="s3a://myBucket/myApp"
I want to get the property from the file instead and concatenate it with /myApp
So I'm getting path to the file like this:
file="/apps/properties/various/s3-apps.properties"
But how do I get the property and concatenate it to construct a path?
I guess that it should be something like this but it did not work for me:
thePath="s3a://{$file.s3Path}/myApp"
Non of the answers from the "Duplicate" helped me in understanding an solving my question. But the #cody answer below was correct and helped.
If you're using GNU grep, the following would work:
$ s3path=$(grep -Po '(?<=s3Path=).+$' "$file")
$ echo $s3path
s3a://myBucket
This question already has answers here:
Shell command to retrieve specific value using pattern
(3 answers)
Closed 8 years ago.
I have file test.txt contains the following
AA=testing
BB=help
CC=hello
How can i make a bash script that will get each value and assign to a new variable?
#!/bin/bash
var1=testing
var2=help
var3=hello
thanks for the help
First of all a = value is not correct syntax in shell. In shell the spaces are important.
When you have a valid file, you can use the eval function to evaluate that file as a string, or simply source it.
This question already has answers here:
What is indirect expansion? What does ${!var*} mean?
(6 answers)
Closed 8 years ago.
I am writing a shell script (#!/bin/sh) which has a variable VAR which contains the name of another variable FOO, which in turn is set to BAR.
FOO=BAR
VAR=FOO
I want to get the value of the variable named in VAR, so something like:
echo "${$VAR}"
But that does not seem to work. Suggestions?
In Bash:
echo "${!VAR}"
Without Bash (though it works in Bash too):
eval echo "\${$VAR}"
Beware: eval is a very general mechanism that can run you into problems very easily. It works fine here, but be cautious about using it more generally.