How to simply combine field separated by a slash [duplicate] - bash

This question already has answers here:
How can I join elements of a Bash array into a delimited string?
(34 answers)
Closed 7 years ago.
How to simply combine field separated by a slash ?
LIST=("a" "b" "c")
STRING=???
echo $STRING
a/b/c
Please someone help? Thank you.

In BASH you can do:
list=("a" "b" "c")
printf -v str "%s/" "${list[#]}"
str="${str%/}"
Check output:
echo "$str"
a/b/c
Avoid all CAPS variables in BASH.
Alternatively using IFS:
str=$(IFS=/; echo "${list[*]}")

Related

Convert string to an array in shell script [duplicate]

This question already has answers here:
Bash: Split string into character array
(20 answers)
Closed 4 months ago.
How can ı convert a string to an array in shell script?
For example i want to put "apple" to an array.
array[0] = a
array[1]=p
array[2]=p
array[3]=l
array[4]=e
I tried a lot of things but none of them worked. I tried to use IFS but i have no space or comma in my word so it didn't work.
Parameter expansion is relevant here: ${#var} gives you the number of characters in var, and ${var:start:len} takes len characters from var, starting at position start.
Combine those two techniques and you get:
#!/usr/bin/env bash
string='apple'
array=( )
for ((i=0; i<${#string}; i++)); do
array[$i]=${string:i:1}
done
declare -p array
...which emits as output:
declare -a array=([0]="a" [1]="p" [2]="p" [3]="l" [4]="e")

Bash initialize variable to a string that contains quotes and backslahes [duplicate]

This question already has answers here:
Setting an argument with bash [duplicate]
(2 answers)
Closed 5 months ago.
I have a string containing quotes and backslahes:
-options \'{"version": "http"}\'
I would like to initialize a variable PARAM with this string.
How this can be done in bash?
I thought of adding it to an array: PARAMS=(-options \'{"version": "http"}\')
but the output I am getting is: -options '{version: http}' i.e. without the slashes.
Expected output: -options \'{"version": "http"}\'
Can someone please suggest?
This looks ok to me.
test="-client-options \\'{\"quic-version\": \"h3\"}\\'"
echo "$test"
t2=("$test" "etc")
echo ${t2[#]}
Escape every inner " and double escape for a persisting escape

Print with space [duplicate]

This question already has answers here:
When to wrap quotes around a shell variable?
(5 answers)
Closed 1 year ago.
How to save a line with spaces without line breaks in a file?
Such as this:
Value2="Server=server1.windows.net,1433;Database=DB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"
using:
printf '%s\n' $Value2 >> "./print.csv"
At present it gets saved as:
Server=server1.windows.net,1433;Database=DB;Persist
Security
Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection
Timeout=30;
The variable $Value2 isn't quoted, and has two separate spaces, so the printf command sees three strings, not one -- so printf prints three strings on three lines. To make printf see just one string, put the variable in quotes, like this:
printf '%s\n' "${Value2}"
Output:
Server=server1.windows.net,1433;Database=DB;Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;
Or, as proof use wc to count the lines:
printf '%s\n' "${Value2}" | wc -l
Output:
1

Print a variable containing spaces in bash [duplicate]

This question already has answers here:
I just assigned a variable, but echo $variable shows something else
(7 answers)
Closed 6 years ago.
I have this string:
string="I love spaces"
I would like to print that so that the spaces would remain. echo $string doesn't seem to print the spaces as well.
Desired Output:
I love spaces
Nevermind me fellas. echo "$string" :/

how to store the awk command output value into some variable in ksh [duplicate]

This question already has answers here:
ksh storing result of a command to a variable
(5 answers)
Closed 8 years ago.
foundflag = awk -F" " 'FNR==NR{A[$arg1 OFS $PREVFILE];next}
!($arg1 OFS $PREVFILE in A){X++} END{if(!X){print "No diff."}
else {print "Diff found."}} $arg1 ${PREVFILE}
echo $foundflag
here am comparing two files and want to store awk command result into some variable please help in Ksh script.
I think what You need is:
foundflag=`awk ....`
remove spaces arounf =
put the command into reverse quotes (``)
Or use command substitution. The backticks/gravemarks have been deprecated in favor of $()
foundflag=$(awk....)
Also note where cannot be any space around the assignment operator (no space before and after)

Resources