Replace substring in string [duplicate] - bash

This question already has answers here:
Search+replace strings in filenames
(2 answers)
How do I set a variable to the output of a command in Bash?
(15 answers)
Command not found error in Bash variable assignment
(5 answers)
Closed 1 year ago.
i have just tried to one of my first bash scripts, i need to find a substring(after the ? part) in a url and replaced with the replace_string,
#!/bin/bash
url="https://example.com/tfzzr?uhg"
# 123456 ...
first= echo `expr index "$url" ?`
last= expr length $url
replace_string="abc"
part_to_be_replace = echo ${url:($first+1):$last}//dont know how to use variable here
substring(url,part_to_be_replace,replace_string)
It does not work, i was able to find only the first accurance of ?, and the length of the string

Does this help?
url="https://example.com/tfzzr?uhg"
replace_string="abc"
echo "${url}"
https://example.com/tfzzr?uhg
echo "${url//\?*/${replace_string}}"
https://example.com/tfzzrabc
# If you still want the "?"
echo "${url//\?*/\?${replace_string}}"
https://example.com/tfzzr?abc
See https://www.gnu.org/software/bash/manual/html_node/Shell-Parameter-Expansion.html for further details.

Use parameter expansion:
#! /bin/bash
url='https://example.com/tfzzr?uhg'
replace_string=abc
new=${url%\?*}?$replace_string
echo "$new"
${url%\?*} removes the pattern (i.e. ? and anything following it) from $url. ? needs to be quoted, otherwise it would match a single character in the pattern. Double the percent sign to remove the longest possible substring, i.e. starting from the first ?.

Related

Is it possible to force bash to do an in-place expansion of a variable whose value contains quotes? [duplicate]

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Why does shell ignore quoting characters in arguments passed to it through variables? [duplicate]
(3 answers)
Closed 1 year ago.
Consider the following simple function.
X() {
echo "$1"
echo "$2"
echo "$3"
}
Now consider the following variable:
args="-H 'h1 : v1'"
When I run the command X $args, I get the following output:
-H
'h1
:
On the other hand, if I run the command X -H 'h1 : v1', I get the following output:
-H
h1 : v1
Note that, in the latter case, the quotes inside the variable's value are correctly interpreted as delimiters.
Is it possible to modify either the declaration of the variable or the invocation with a variable to force the two outputs above to be equivalent?
This is what arrays are for.
$ args=(-H 'h1: v1')
$ printf '%s\n' "${args[#]}"
-H
h1 : v1

Sending a string with quotation mark in bash script [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 2 years ago.
How to pass whole string also with quotation marks.
#!/bin/bash
my_name="My Value"
function abc(){
a=$1
echo $a
}
abc $my_name
This gives :
My
How to get the value as :
"My value"
also with quotation marks
Try enclosing in quotes instead of abc $my_name
abc "$my_name"
If you want quotes in your output too then try defining as:
my_name="\"My Value\""
and then type:
abc "$my_name"
The easiest way to do this is to pass your argument within single quotes. This will consider your whole argument as a single string.
Try this:
#!/bin/bash
my_name='"My Value"'
function abc(){
a=$1
echo "$a"
}
abc "$my_name"

Populate bash array from a string containing escaped space [duplicate]

This question already has answers here:
Reading quoted/escaped arguments correctly from a string
(4 answers)
Honoring quotes while reading shell arguments from a file
(1 answer)
Closed 4 years ago.
I want to populate a bash array by splitting one of the arguments based on the space but preserving the escaped space or double quotes.
populate_array() {
params="${1}"
array=(-j $params)
for elem in "${array[#]}"
do
echo "${elem}"
done
}
cols="x\ y"
populate_array "${cols}"
Output:
-j
x\
y
Desired output:
-j
x y
I even tried escaped double quotes
populate_array() {
params="${1}"
array=(-j $params)
for elem in "${array[#]}"
do
echo "${elem}"
done
}
cols="\"x y\""
populate_array "${cols}"
Output:
-j
"x
y"
Desired output:
-j
x y
FYI it can be easily done using eval, but I'd rather prefer not to do that.
The answers https://stackoverflow.com/a/31485948/3086551 explain by either taking human generated input or reading from the file. I want to parse the passed argument with escaped space or double quotes instead.

How to understand variable and variable's value 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 5 years ago.
#!/bin/bash
list="one two three"
one=1
two=2
three=3
for k in $list
do
echo $k
done
For the code above, output is:
one
two
three
But I always think it should output:
1
2
3
It's confusing. How to understand this?
The expansion $k just gives you the name of the variable as a string. If you want the value of the variable, you must use the parameter expansion syntax ${!k}. This is documented here.
#!/bin/bash
list="one two three"
one=1
two=2
three=3
for k in $list
do
echo "${!k}"
done
Output
1
2
3

What does ${MY_VAR:-1} mean in bash? [duplicate]

This question already has answers here:
How does Bash parameter expansion work? [duplicate]
(2 answers)
Closed 7 years ago.
I saw this mentioned in some documentation. But it doesn't seem to do anything...
~ $ echo $DYNO
run.9917
~ $ echo ${DYNO}
run.9917
~ $ echo ${DYNO:-1}
run.9917
What does the :-1 do?
${DYNO:-1} means if DYNO exists and isn’t null, return its value; otherwise return 1. It's the :- between the variable's name and the default value inside the {} that make this happen.
This is covered in the Shell Parameter Section of the Bash Reference Manual.
"${var:-1}" means expand the parameter named var if it's defined, and if not, expand 1 instead.
Other, similar expansions:
"${var: -1}" means expand the substring of var from the last character.
"${var:=1}" means assign 1 to var if it's not defined and then, either way, expand the parameter.
Examples:
$ x=hi y=
$ echo "${x:-1}" "${x: -1}" "${y:-2}"
hi i 2
${var:-word} If var is null or unset, word is substituted for var. The value of var does not change.
$> echo "${bar:-1}"
$> 1
$> bar=3
$> echo "${bar:-1}"
$> 3
$> echo "${bar-1}"
$> 3

Resources