How to echo string with variable substitution in bash? [duplicate] - bash

This question already has answers here:
Forcing bash to expand variables in a string loaded from a file
(13 answers)
Closed 3 years ago.
I have a string like ${REPOSITORY}/company/api:${API_VERSION}. $REPOSITORY and $API_VERSION are shell variables.
$ echo ${DATA_API_VERSION}
latest
$ echo ${REPOSITORY}
com.company.repo
I want to get the interpolated string that shows the values of these variables and assign it to another variable.
This is what I get:
$ echo "$image"
${REPOSITORY}/company/api:${API_VERSION}
I want this:
com.company.repo/company/api:latest

You could use sed to search and replace the two variables.
#!/bin/bash
DATA_API_VERSION="latest"
REPOSITORY="com.company.repo"
image='${REPOSITORY}/company/api:${DATA_API_VERSION}'
sed -e "
s/\${REPOSITORY}/$REPOSITORY/g
s/\${DATA_API_VERSION}/$DATA_API_VERSION/g
" <<< "$image"

Related

In Shell script process variables in text [duplicate]

This question already has answers here:
bash replace variable name in string with variable value
(2 answers)
Closed 2 years ago.
I have a file 'test.txt', the contents is "SomeText_$(date '+%Y%m%d')".
When reading this into a variable with:
txt=`cat test.txt`
Then I try to print with
echo $txt
This prints: "SomeText_$(date '+%Y%m%d')"
How do I print this so I receive "SomeText_20200904"
The posted echo will display the content from $txt but not execute anything else.
The second line here with eval will read and process what follows then execute the result as a shell command
txt=`cat test.txt`
eval echo $txt

Store into var the result of grep command shell script [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
How to pass the value of a variable to the standard input of a command?
(9 answers)
Closed 3 years ago.
I'am creating a shell script to extract a number from a particular line, where one particular string appears (isDone), for that i use a grep, i find the line and can echo it, but i can't store the grep output to a var.
$text var got inumerous tags, one of them having the string "isDone", thats the line i want:
code:
short_str="isDone"
echo "$text" | grep "$short_str"
output:
< s:key name="isDone">1</s:key >
now i want to store that output from grep into a file, and then extract the value (on this case is 1)
what have i tried:
store="$("$text" | grep "$short_str")"
echo "$store"
but that outputs all the file, what am i doing wrong?

bash- Assigning a value to a string using a command (rev) [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 4 years ago.
I am trying to assign the value of a string to the reversed value of another string (via the 'rev' command). I know the rev command is used like this:
$ echo "hello" | rev
olleh
But what I am trying to do is something like this:
var="hello"
rav=${$var | rev}
I know this isn't correct syntax and it doesn't work, but I was wondering if there was a way to assign a variable using a command, and if so how is it done?
Try this, using command substitution:
var="hello"
rav=$(echo "$var" | rev)
echo $rav
Following may also help you on same.
var="hello"
rav=$(rev <<< "$var")
Output will be as follows:
echo $rav
olleh

awk command does not work properly when assigning output to variable [duplicate]

This question already has answers here:
How do I set a variable to the output of a command in Bash?
(15 answers)
Closed 5 years ago.
I'm trying to convert space to underscore in a file name, my script is
like below.
old_file=/home/somedir/otherdir/foobar 20170919.csv
new_file="$(basename "$old_file")" | awk 'gsub(" ","_")'
This script works fine when I use with echo command,
echo "$(basename "$old_file")" | awk 'gsub(" ","_")'
but when it comes to assigning the output to variables, it doesn't work...
Does anybody know the idea?
Actually no need of awk, please note below one replaces all space to underscore, not just filename, it can be path too
$ old_file="/home/somedir/otherdir/foobar 20170919.csv"
$ newfile="${old_file// /_}"
$ echo "$newfile"
/home/somedir/otherdir/foobar_20170919.csv

Why environment variables aren't being passed in a single command [duplicate]

This question already has answers here:
Why can't I specify an environment variable and echo it in the same command line?
(9 answers)
Closed 7 years ago.
In bash, I can pass an environment variable to a single command in the following way:
KEY=VAL <command>
However, I don't understand why the following doesn't work:
KEY=VAL echo $KEY
While this works:
KEY=VAL bash -c 'echo $KEY'
i.e. the first one prints a blank line while the other prints "VAL". I'd expect both to print "VAL".
Because KEY=VAL echo $KEY isn't having echo expand the $KEY variable.
The current shell is doing that before it runs echo (or whatever).

Resources