Can Windows Environment Variables Be Set to Integers/Numbers [duplicate] - windows

This question already has answers here:
Powershell strongly typed environment variable
(1 answer)
Cannot filter Directories with Powershell using an environment variable
(1 answer)
Closed 6 months ago.
I am new to working with the Windows environment so I wanted to know if it was possible to set the value of an environment variable to a number (like an int) or something other than a string. I am trying to do this via a PowerShell script using the [Environment]::SetEnvironmentVariable() method. For example, if I wanted to create an environment variable to track the number of entries in my PATH I would do something like this:
$count = 0
$pathEntries = #([Environment]::GetEnvironmentVariable("PATH", "User") -split ";")
foreach ($entry in $pathEntries){
$count += 1
}
[Environment]::SetEnvironmentVariable("PATH_COUNT", $count, "User")
I am using PATH as an example and I know this is probably not the best way to do this but I just wanted to know if Windows environment variables could hold integer or other numeric data types.

Related

Combine two variables to form the identifier for another variable in Bash [duplicate]

This question already has answers here:
How to get a variable value if variable name is stored as string?
(10 answers)
Closed 1 year ago.
I want to be able to take the values of two variables and concatenate them together to form the identifier for another variable in a bash script.
final_answer="we did it"
one="final"
two="answer"
t="${one}_${two}"
echo ${$t} # would like this to echo we did it; currently give "${$t}: bad substitution"
Not sure this is possible but it seems like bash would have this capacity somehow.
Thank you!
$ echo "${!t}"
we did it
See http://mywiki.wooledge.org/BashFAQ/006#Indirection for details.

Passing value to an existing variable in makefile [duplicate]

This question already has answers here:
Passing additional variables from command line to make
(8 answers)
Closed 3 years ago.
I found this port = ${PORT} in one makefile. How can I pass value to the variable port using make commands?
for many simple situations, pass the values on the command line using the variable=value grammar. Note that this will (usually) override the value in the Makefile.
make PORT=1234 ...
# OR
make port=1234 ...
This format will also override other variable settings (environment variables, built-in variables, ...). It will NOT replace variables set with the 'override' directive.
#rveerd comment provide a link to more complete answer.

Is it allowed to have windows environment variable name to have dot? [duplicate]

This question already has answers here:
Environment variables in PowerShell with a dot (.) in the name
(3 answers)
Closed 3 years ago.
I tried to set environment variable as aaa.bbb in powershell from windows platform however it looks like it does not like ..
This works.
$Env:aaa = "testvalue"
This fails.
$Env:aaa.bbb = "testvalue2"
Is it possible to have environment variable name to have .?
You can set the desired variable by enclosing it in brackets like this:
${Env:aaa.bbb} = "testvalue2"
Note that you'll need to reference the variable by including the brackets but tab completion helps(at least on PowerShell 5.1).
Slightly less cluttered in appearance is the New-Item/Set-Item as mentioned by eryksun:
New-Item -Path Env:aaa.bbb -Value "testvalue2"
I see the option [Environment]::SetEnvironmentVariable Jeroen Mostert provided has done the trick for you, per the comments.

Extract the pointer from a V_${i}_T variable name in a bash loop [duplicate]

This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
Suppose I have several variable names like
V_1_T = a
V_2_T = b
V_3_T = c
...
and I want to extract the pointers a, b , c, ... in a bash loop in order to concatenate the values. My explicit wish is about reconstructing a message separated in several parts as explained in the gammu-smsd documentation.
I've tried the example in the doc but it doesn't work. The reason is that the code never points to the pointer of the variables but to the variables themselves, i.e. I get V_1_T at best and never a as I would.
I've also tried to put
${V_${i}_T} ; ""$"V_${i}_T"
with and without escape symbols for the commas, ..., but nothing worked ...
Any ideas ?
I'm working on the latest version of Raspbian + RaspberryPi.
Use indirect parameter expansion:
for i in 1 2 3; do
t="V_${i}_t"
echo "${!t}"
done
This avoids the use of eval shown in the docs you linked to.

how can I read a value from an environment variable with ':' in its name? [duplicate]

This question already has an answer here:
Accessing environment variables that don't map to valid shell variable names
(1 answer)
Closed 5 years ago.
We are using elastic beanstalk. Some values are environment properties of the environment. When I perform a container_command I'm able to read this properties as environment variables. The problem is the following: a lot of properties are named like this db:user or collector:server and after that the value.
How can I read this values? I can interpret them as environment variables. So the environment properties with 'normal' names I can read. But not those ones who contain a ':' in their name:
To test (+ make it clearer for people who don't know elastic beanstalk) I've created this. The global goal is to read the value of a variable which contains a ':' in its name.
#!/bin/bash
${myvar:test}="hey"
echo ${myvar:test}
$./test.sh
$./test.sh: line 3: =hey: command not found
: isn't an allowed character in shell variables at all, and ${test:foo} has a completely separate meaning (it expands $test with a default value of foo if no variable named test is defined).
If your operating system is Linux, however, you can directly parse your original environment variables from procfs:
#!/usr/bin/env bash
declare -A env=( ) ## <- note that this requires bash 4.0 or newer
while IFS= read -r -d '' envvar; do
[[ $envvar = *:* ]] || continue
varname=${envvar%%=*}
value=${envvar#*=}
env[$varname]=$value
done < /proc/self/environ
echo "The value of the environment variable db:user is: <<${env[db:user]}>>"
Note that to test this, though, you'll need to be able to actually create an environment variable with a literal colon, and your code currently fails at doing so. Consider instead:
env db:user="test value for db:user" ./yourscript

Resources