This question already has answers here:
When do we need curly braces around shell variables?
(7 answers)
Closed 7 years ago.
Please help me understand and possibly explain this.
Suppose I have 2 env variables:
$REL --> foo
$MEL --> bar
Now if I echo $REL$MEL it works.
If I echo test$REL, it works.(testfoo)
But why Linux is not able to identify the variable when used like this: $RELtest?
I understand this looks like the stupidest question but is there a way to tell the system to look for the matching part of the string in env variables already set and replace that part?
I also understand that when doing test$REL, it is $ that works as an identifier.
If this is not possible then how to explain the limitation?
Any usage example in docs where this is shown prohibited will help me a lot.
With $RELtest, the shell interprets it as the value of the variable RELtest - it is not able to figure out that you meant REL followed by a literal test since there is no delimiter. You need to be more explicit - use {} to quote the variable:
$ echo ${REL}test
footest
See also When do we need curly braces in variables using Bash?
Related
This question already has answers here:
Why does passing variables to subprocess.Popen not work despite passing a list of arguments?
(5 answers)
Closed 1 year ago.
I have a variable in python, and I'm trying to open a subprocess and echo the variable, then create a file with the variable in it.
I tried this:
subprocess.Popen(['echo "$var" > file.txt'], shell=True)
It creates the file, but it's empty. How can I get the result that I want?
In Python you don't use $ sign to use a variable. Also when you want to embed variable into string, you cannot just simply use variable name in string. You should do something like that:
subprocess.Popen(['echo "{}" > file.txt'.format(var)], shell=True)
This is great website which will explain you how to use .format method.
This question already has answers here:
Bash - variable variables [duplicate]
(4 answers)
Lookup shell variables by name, indirectly [duplicate]
(5 answers)
How to use a variable's value as another variable's name in bash [duplicate]
(6 answers)
Is it possible to build variable names from other variables in bash? [duplicate]
(7 answers)
Closed 5 years ago.
I'm trying to get get the VALUE of a 'nested' variable into another variable and/or use the value directly as shown below
Below is an example scenario which exactly explains where I'm stuck
$ USER1_DIR=./user1/stuff
$ USER2_DIR=./user2/stuff
$ USER3_DIR=./user3/stuff
#User will be taken as input, for now assuming user is USER1
$ USER="USER1"
$ DIR=${USER}_DIR
$ echo $DIR
>> USER1_DIR
$ DIR=${${USER}_DIR}
>> -bash: ${${USER}_DIR}: bad substitution
Challenge 1:
Get DIR value to ./user1/stuff when the input is USER1
or
Get ./user1/stuff as output when the input is USER1
After I'm able to get through Challenge 1, I've to add some content to a file in the user directory like below
Desired output is as below
$ echo "Some stuff of user1" >> $DIR/${DOC}$NO
# Lets say DOC="DOC1" and NO="-346"
# So the content has to be added to ./user1/stuff/DOC1-346
# Assume that all Directories exists
FYI, The above code will be a part of a function in a bash script and it will be executed only on a Linux server.
Note : I don't know what to call variable DIR hence used the term 'nested' variable. It would be great to know what is it called, greatly appreciate any insight. :)
You can use eval, variable indirection ${!...}, or reference variables declare -n.
In the following, I will use lowercase variable names, since uppercase variable names are special by convention. Especially overwriting $USER is bad, because that variable normally contains your user name (without explicitly setting it). For the following code fragments assume the following variables:
user1_dir=./user1/stuff
user=user1
Eval
eval "echo \${${user}_dir}"
# prints `./user1/stuff`
Eval is a bash built-in that executes its arguments as if they were entered in bash itself. Here, eval is called with the argument echo "${user1_dir}".
Using eval is considered bad practice, see this question.
Variable Indirection
When storing the name of variable var1 inside another variable var2, you can use the indirection ${!var2} to get the value of var1.
userdir="${user}_dir"
echo "${!userdir}"
# prints `./user1/stuff`
Reference Variables
Instead of using indirection every time, you also can declare a reference variable in bash:
declare -n myref="${user}_dir"
The reference can be used similar to variable indirection, but without having to write the !.
echo "$myref"
# prints `./user1/stuff`
Alternatives
Your script may become easier when using (associative) arrays. Arrays are variables that store multiple values. Single values can be accessed by using an index. Normal arrays use natural numbers as indices. Associative arrays use arbitrary strings as indices.
(Normal) Arrays
# Create an array with three entries
myarray=(./user1/stuff ./user2/stuff ./user3/stuff)
# Get the first entry
echo "${myarray[0]}"
# Get the *n*-th entry
n=2
echo "${myarray[$n]}"
Associative Arrays
Declare an associative array with three entries
# Create an associative array with three entries
declare -A myarray
myarray[user1]=./user1/stuff
myarray[user2]=./user2/stuff
myarray[user3]=./user3/stuff
# Get a fixed entry
echo "${myarray[user1]}"
# Get a variable entry
user=user1
echo "${myarray[$user]}"
This question already has answers here:
Brace expansion with variable? [duplicate]
(6 answers)
Closed 7 years ago.
I would like to use the numeric value of the input argument that I pass to a bash script like this:
./script_pb3.sh out.txt 3
Here I would like to take the second argument that I pass (i.e. the value 3) and use it as a numeric value inside my script, that looks like this:
#!/bin/bash
NUM=$2
for i in {1..$NUM}; do
echo $i
done
But when I run it all I get is
{1..3}
Instead of
1
2
3
Can you please provide an explanation for why this is happening and a workaround? I think that this question aims at casting a string value to an integer value but I am not sure. Any help is greatly appreciated.
EDIT: the answer from here contains a debate concerning the answer to my issue (i.e. it assumes the answer is already known), but it does not come as an answer to my question, therefore I didn't find it in my first searches. Thank you, though, for pointing it out.
I think you have to use seq
for i in `seq 1 10`;
do
echo $i
done
Reference: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
So, what you're looking for is : seq 1 $2
I've seen a couple posts for this, like this one, but none are helping me in my particular situation.
scriptsPath="/var/db/gbi/scripts/"
echo "$scriptsPathawesome.csv";
I would expect this to echo /var/db/gbi/scripts/awesome.csv
Instead I get .csv
Seems like it thinks I'm trying to reference a variable named $scriptsPathawesome. How can I concatenate the $scriptsPath variable to the "awesome.csv" string literal?
You need to surround your variable with curly braces like so:
scriptsPath="/var/db/gbi/scripts/"
echo "${scriptsPath}awesome.csv";
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Annoying PHP error: “Strict Standards: Only variables should be passed by reference in”
I have this line of code,
$extension=end(explode(".", $srcName));
when I fun my function I get
PHP Strict Standards: Only variables should be passed by reference in
I am not sure how to solve this
The function end() requires a variable to be passed-by-reference and passing the return-value of a function doesn't acheive this. You'll need to use two lines to accomplish this:
$exploded = explode(".", $srcName);
$extension = end($exploded);
If you're simply trying to get a file-extension, you could also use substr() and strrpos() to do it in one line:
$extension = substr($srcName, strrpos($srcName, '.'));
Or, if you know the number of .'s that appear in the string, say it's only 1, you can use list() (but this won't work if there is a dynamic number of .'s:
list(,$extension) = explode('.', $srcName);