I've got a variable, let's say $x and it holds the value of website.com. I want to be able to call the variable and apply shell color to it like so:
echo -e '\033[1;32m$x:\033[0m';
The problem is not the color, however, it's how the script it interpretting the output. So the output I'm getting is:
$x:
I need the output to obviously be the string in the variable, and not the variable name. Is there any way around this issue?
You need to use " instead of '.
So it should be: echo -e "\033[1;32m$x:\033[0m";
Variables are generally interpolated inside double quotes.
Related
This is my script code
#!/bin/bash
timestamp=$(date +%F-%T)
clinet_id="123"
STRING=s3://<bucketname>/folder/$client_id/$client_id_gdpr_access_report_$timestamp.csv
echo "$STRING"
$SHELL
If i run this code am getting timestamp value.csv file
how can i concatenate variable with string.
am expecting out put like below
s3://<bucketname>/folder/123/123_report_2022-01-25-14:55:47.csv
i can able to concatenateaccess_report_$timestamp.csv
if i add $client_id_ in the beginning, it will print
2022-01-25-14:55:47.csv
Expecting a better advice
You need to look better at the names of your variables; it's 'client_id' not 'clinet_id' ...
And you should take care of double quoting your string, and put braces around variables when in doubt:
STRING="s3://<bucketname>/folder/${client_id}/${client_id}_gdpr_access_report_${timestamp}.csv"
I want to define variable in shell script as:
value1 = 40 (this can be number or character)
and want to use as in a text like:
$value1_position.xyz (I basically want 40_position.xyz)
How do I do this?
this should do:
${value1}_position.xyz
beware that the variable should be declared with this syntax
value1=40
notice the absence of spaces around the =
To define a variable, simply make sure there are no spaces between the variable name and value
value1=40
To use that variable in bash substitution, creating what you want, use the $ replacement symbol like so:
${value1}_position.xyz
To append that to your text file
echo "${value1}_position.xyz" >> file.txt
Here is a simple bash script:
a="asd"
b="qf"
echo "$a.$b"
echo "$a_$b"
It's output is:
asd.qf
qf
Why the second line is not "asd_qf" but "qf"?
Because you haven't defined a variable named a_. For that second printout to work, use:
echo "${a}_$b"
Your second echo displays the value of variable $a_ which is unset.
Use echo "${a}_$b"
The shell has rules about what can go in a variable name, and $a_ is interpreted as the variable named a_ (there is no variable with that name so its value is empty).
You can always add braces to be explicit. In this case, ${a}_$b will clearly identify what the variable name is and the result will be what you expect.
I have multiple variables in a shell script; i was trying to save some code duplication and wanted to do something like following
# variables
FLAG=SIM
SIM_ICR_KEY_VAL="http://www.example.com/simi/icr"
REAL_ICR_KEY_VAL="http://www.example.com/real"
Based on the FLAG value i want to access the correct variable (without using IF's)
When i try this it echos the variable name & not the value itself.
echo $(echo ${FLAG}_ICR_KEY_VAL)
On further note; i need to use these substitutions inline in a sed statememt:
sed "s!${ISTR_KEY}=.*!${ISTR_KEY}=${SIM_ISTR_KEY_VAL}!" > tmp.file
... i am not sure its possible or not, please suggest
Reflection can be achieved with the infamous eval:
eval thisvar=\$${FLAG}_INC_KEY_VAL;
echo "We are using $thisvar"
Whenever you find yourself dynamically synthesizing a variable name, though, you are probably Doing It Wrong. You should consider alternatives like arrays:
ICR_KEY_VAL[0]="http://www.example.com/simi/icr"
ICR_KEY_VAL[1]="http://www.example.com/real"
SIM=0
echo ${ICR_KEY_VAL[$SIM]}
I don't know how to do it directly, but in bash you can do it indirectly:
FLAG=SIM
SIM_ICR_KEY_VAL="http://www.example.com/simi/icr"
REAL_ICR_KEY_VAL="http://www.example.com/real"
FLAG_ICR_KEY_VAL=${FLAG}_ICR_KEY_VAL
sed "s!${ISTR_KEY}=.*!${ISTR_KEY}=${!FLAG_ISTR_KEY_VAL}!" > tmp.file
I have an environment variable called $TEST which refers to a directory
in my bash script I have a variable called $VARTEST which is $TEST/dir/file
now I want to grep the file specified by $VARTEST so I try to do:
grep somestring $VARTEST but it doesn't translate $TEST into it's directory
I've tried different combinations of {}, "" and '' but without success
I think you want
eval grep somestring "$VARTEST"
or even
VARTEST_EVALUATED=$(eval echo $VARTEST)
grep "$SOMESTRING" "$VARTEST_EVALUATED"
but remember (as others already said): If possible use
VARTEST="$TEST/foo/bar"
instead of
VARTEST='$TEST/foo/bar'
use the second one only if you really need kind of 'lazy evaluation'...
Warning, this could be dangerous if $VARTEST contains malicous code.
Have you put single quotes around something? Single quotes will prevent the variables from being translated into their corresponding values. Double quotes will work though. For example:
#!/bin/sh
TEST="/etc"
VARTEST="$TEST/passwd"
grep "$LOGNAME" "$VARTEST"