Inserting Variable into sh Script Command [duplicate] - shell

This question already has answers here:
Filename not printing correctly with underscore "_" in Bash [duplicate]
(2 answers)
Error in string Concatenation in Shell Scripting
(3 answers)
bash variable interpolation separate variables by a hyphen or underscore
(3 answers)
When do we need curly braces around shell variables?
(7 answers)
Closed 5 years ago.
#!/bin/sh -f
set proj_dir="OutputDir"
for projname in lib proj1 proj2
do
mv ./scripts/$projname_BYTECODE ./$proj_dir/scripts/$projname
done
A very simple example of what is not working well for me. $projname_BYTECODE is being interpreted as a variable name but _BYTECODE is actually part of the folder name. Suggestions?

Use ${X} instead of $X, so in your example ${projname}_BYTECODE should do the trick. Have a look at this question for more information: When do we need curly braces in variables using Bash?

Related

bash script, want to use '$' without it looking for a variable [duplicate]

This question already has answers here:
Interpolating variables which contain '$' in a bash script
(1 answer)
Difference between single and double quotes in Bash
(7 answers)
Closed 2 years ago.
I need to write a cron job, to send an mqtt message to "homie/$HOSTNAME/$state".
In the case of $HOSTNAME - it needs to use the env variable, but $state - it must use as is.
How would I add "homie/$HOSTNAME/$state" without bash thinking the $state is a variable as well?

Bash -Bad substitution error when evaluating two env variables [duplicate]

This question already has answers here:
creating environment variable with user-defined name - indirect variable expansion
(1 answer)
Dynamic variable names in Bash
(19 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 3 years ago.
This is my script:
#!/bin/bash
AREA="DEV"
DEV_AREA_USER="DevAdmin"
TEST_AREA_USER="TestAdmin"
TEST=${$AREA_AREA_USER}
echo ${TEST}
Result: Bad substitution error. Expected result: DevAdmin
How to fix this? I do not want to create a new variable because there are 75 such variables and 75 files to edit. The lesser the better...

Expand bash variable within quotes [duplicate]

This question already has answers here:
Expansion of variables inside single quotes in a command in Bash
(8 answers)
How to echo variable inside single quotes using Bash?
(2 answers)
Closed 5 years ago.
How can I expand APP_VERSION so that it gets used in the rest of my script? As it is right now, it doesn't expand and show the argument I pass in via the CLI.
APP_VERSION=$2
BOOTSTRAP_RUN='[{"Path":"s3://abc/scripts/emr/deploy_zip.sh ${APP_VERSION}","Name":"Custom action"}]'

bash bad substitution - access an environment variable from another [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Reference an appended variable?
(3 answers)
Closed 5 years ago.
How can I access an environment variable from another? I have the following in my shell
#!/usr/bin/env bash
set -x
export A_version=1.0.0
component=A
echo ${${component}_version}}
the bash script after the run gives me
temp.sh: line 9: ${${component}_version}}: bad substitution
You can use eval to do this. Here is a working version of your script that prints 1.0.0:
export A_version=1.0.0
component=A
eval "echo \$${component}_version"
For more information, see this page:
http://tldp.org/LDP/abs/html/ivr.html
Update: A safer way to do the same thing in Bash would be:
export A_version=1.0.0
component=A
var=${component}_version; echo "${!var}"
Note that you have to run this script with bash, not sh.

Bash variable expansion in command [duplicate]

This question already has answers here:
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Difference between single and double quotes in Bash
(7 answers)
Closed 4 years ago.
DATE="1 week ago"
date --date='$DATE'
doesn't work. How can I get it to work?
I could do:
DATE_CMD="date --date='$DATE'"
eval $DATE_CMD
but I don't want to store the entire command in a variable.
You're a victim of quote expansion.
The proper invocation would likely be:
DATE='1 week ago'
date --date="$DATE"
(notice the double quotes)
You just need to use double-quotes to enable string interpolation:
date --date="$date"

Resources