This question already has answers here:
Can a shell script set environment variables of the calling shell? [duplicate]
(20 answers)
What does the command 'source' do?
(1 answer)
What is the difference between using `sh` and `source`?
(5 answers)
Closed 3 years ago.
What is the difference between the following two commands to source a file and then print the env variables?
sh -c . envvars.sh; env
And:
./envvars.sh; env
More specifically, what do the following do:
". envvars.sh" vs. "./envvars.sh"
And:
"sh -c envvars.sh" and "./envvars.sh"
Related
This question already has answers here:
How do I use shell variables in Makefile actions?
(2 answers)
Using bash variables in Makefile
(3 answers)
Closed 5 months ago.
I have a simple makefile
SHELL := /bin/bash
test:
VAR='test' && echo ${VAR};
However, this does not exhibit the same behavior as typing this command into a normal shell, and it doesn't echo anything. Not sure what I am missing.
This question already has answers here:
Difference between sh and Bash
(11 answers)
$BASH_VERSION reports old version of bash on macOS, is this a problem that should be fixed?
(4 answers)
Closed 10 months ago.
I have some code that looks like this:
export AWS_REGION='us-east-1'
export CLUSTER_NAME='my_cluster'
export PROJECT_NAME='my_project'
source ./utils.sh
...additional functions...
annotate_cluster # comes from utils.sh
annotate_cluster, which comes from utils.sh, relies on the environment variable PROJECT_NAME. However, when I run it, it complains _utils.sh: line 60: FOO-${PROJECT_NAME^^}: bad substitution. Why can it not access the environment variable I have set?
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...
This question already has answers here:
What is the difference between $(command) and `command` in shell programming?
(6 answers)
What is the benefit of using $() instead of backticks in shell scripts? [duplicate]
(9 answers)
Closed 3 years ago.
In Unix we have 2 ways to execute a command and capture its output in a variable:
1.)
x=`wc-l`
2.)
x=$(wc -l)
Can anyone help me understand the basic difference between the two, and when to use which syntax.
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.