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

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.

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

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"

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 loop through all chars in string [duplicate]

This question already has answers here:
How to perform a for loop on each character in a string in Bash?
(16 answers)
Closed 7 years ago.
How should I loop through all chars in a string.
My pseudocode
stringVar="abcde"
for var in stringvar
{
do some things with var
}
result i need
a
b
c
d
I want to loop all the vars but i can only get it to work with a whitespace splitted var like
stringVar="a b c"
for var in stringVar; do
echo $var
done;
result
a
b
c
but i cant do it for a string that isnt split with whitespaces.
The question is flagged as duplicate but not one of the answers (with upvotes) is available in the linked questions..
You can use read for that:
string="abcde"
while read -n 1 char ; do
echo "$char"
done <<< "$string"
Using -n 1 read will read one character at a time. However, the input redirection <<< adds a newline to the end of $string.
stringVar="abcde"
for ((i=1;i<=${#stringVar};i++)); do
echo ${stringVar:$i-1:1}
done
Output:
a
b
c
d
e

Resources