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

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"

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