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

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

Related

Replace substring in string [duplicate]

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 ?.

assigning name to text file in shell script [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

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 do double variable substitution inside bash if loop [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

Bash - variable variables [duplicate]

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 4 years ago.
I have the variable $foo="something" and would like to use:
bar="foo"; echo $($bar)
to get "something" echoed.
In bash, you can use ${!variable} to use variable variables.
foo="something"
bar="foo"
echo "${!bar}"
# something
eval echo \"\$$bar\" would do it.
The accepted answer is great. However, #Edison asked how to do the same for arrays. The trick is that you want your variable holding the "[#]", so that the array is expanded with the "!". Check out this function to dump variables:
$ function dump_variables() {
for var in "$#"; do
echo "$var=${!var}"
done
}
$ STRING="Hello World"
$ ARRAY=("ab" "cd")
$ dump_variables STRING ARRAY ARRAY[#]
This outputs:
STRING=Hello World
ARRAY=ab
ARRAY[#]=ab cd
When given as just ARRAY, the first element is shown as that's what's expanded by the !. By giving the ARRAY[#] format, you get the array and all its values expanded.
To make it more clear how to do it with arrays:
arr=( 'a' 'b' 'c' )
# construct a var assigning the string representation
# of the variable (array) as its value:
var=arr[#]
echo "${!var}"

Resources