I have a variable called filepath=/tmp/name.
To access the variable, I know that I can do this: $filepath
In my shell script I attempted to do something like this (the backticks are intended)
`tail -1 $filepath_newstap.sh`
This line fails, duuh!, because the variable is not called $filepath_newstap.sh
How do I append _newstap.sh to the variable name?
Please note that backticks are intended for the expression evaluation.
Use
"$filepath"_newstap.sh
or
${filepath}_newstap.sh
or
$filepath\_newstap.sh
_ is a valid character in identifiers. Dot is not, so the shell tried to interpolate $filepath_newstap.
You can use set -u to make the shell exit with an error when you reference an undefined variable.
Use curly braces around the variable name:
`tail -1 ${filepath}_newstap.sh`
In Bash:
tail -1 ${filepath}_newstap.sh
Related
myDir = 'apple'
If I have the above variable, what functionality can I use to use it within a command? (I'm guessing it's some type of substitution - so I'd like to know what it's called if so)
How could I use the above variable to do an ls /home/applefruit, as obviously ls /home/$myDirfruit does not work.
First of all, your variable declaration is not right. There must be no spaces around = in declaration:
myDir='apple'
Now, ls /home/$myDirfruit did not work because myDirfruit is being treated as the variable name instead of just myDir. You need to use {} to enclose the variable name when the name is being followed by valid variable name constituent character:
ls /home/${myDir}fruit
would be expanded to:
ls /home/applefruit
Also if you have spaces in variable name e.g. myDir='foo bar', use quotes around variable:
ls /home/"${myDir}"fruit
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
I know the basics of Bash but often miss the nuance and I'm having a problem using it to achieve what I had hoped would be a rather simple problem:
If I have the following in a bash script, which works exactly as I'd want it to:
cbType=`echo $configuration | jsawk -a 'return _.where(this,{name: "reference_data"})'`
It takes $configuration -- which is a JSON string -- and identifies the array element where name is "reference_data" and returns that object/hash definition only. Please note that this does use the very handy jsawk utility but it has been designed to be exhibit good command-line behaviour.
The problem is that when I remove the hard-coded "reference-data" with a variable it seems to not be able to reference the scope of the variable. So for instance, ...
myVar="\"reference_data\""
cbType=`echo $configuration | jsawk -a 'return _.where(this,{name: $myVar})'`
Does not work and instead returns a jsawk error of:
jsawk: js error: ReferenceError: $myVar is not defined
Is there anything I can do to enforce that first the variable is expanded, and then the command string is executed?
Declared variables won't be expanded if it's not within double quotes. So put your code inside double quotes instead of single quotes.
myVar="\"reference_data\""
cbType=$(echo "$configuration" | jsawk -a "return _.where(this,{name: $myVar})")
I would like to know if I can substitute a variable twice.
For example:
#global variable
TEST_SERV_EXT=""
#variables become from myconf.sh
TEST_SERV_EXT_FO='foo01'
TEST_SERV_EXT_BR='bar01'
I want dynamically construct those last two and assign them in TEST_SERV_EXT.
I tried something like this ${$TEST_SERV_COMP} but I'm getting "bad substitution" message.
I need something like php's feature "$$" or tcl's subst command.
Regards,
thandem
TEST_SERV_COMP=TEST_SERV_EXT_FO
TEST_SERV_EXT=${!TEST_SERV_COMP}
Look for indirect expansion in the bash manual.
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.