Declaring a variable name (not value) in bash with string substitution [duplicate] - bash

This question already has answers here:
Dynamic variable names in Bash
(19 answers)
Closed 1 year ago.
I have a set of variables whose values I need to modify in a loop, if a match is found in an array which I am reading later. The array elements are substrings of the variable names, so I thought of writing a generic for loop, which would modify the variable values on the fly, attempting string substitution in the variable names, but that doesn't work.
Here is the code:
CUT_APPLE=false
CUT_GUAVA=false
CUT_MANGO=false
AVAILABLE_FRUITS=("APPLE" "GUAVA")
for i in "${AVAILABLE_FRUITS[#]}"; do
CUT_$i=true
done
**CUT_APPLE=true: command not found**
**CUT_GUAVA=true: command not found**
Is there any way using which I can get interpolation in variable name declaration to work?

If your Bash version is greater than 4, it supports associative arrays which make it easy to check whether a boolean key exists:
#!/usr/bin/env bash
# Associative array to store keys of available fruits
declare -A cut_fruit=()
declare -a available_fruits=('APPLE' 'GUAVA' 'RED BANANA')
for fruit in "${available_fruits[#]}"; do
# Create key for fruit in Associative array if available
# shellcheck disable=SC2034 # cut_fruit is indeed used
cut_fruit[$fruit]=
done
# Check if cut fruit is available by checking key existence
for fruit in 'APPLE' 'BANANA' 'RED BANANA' 'MANGO'; do
if [[ -v cut_fruit[$fruit] ]]; then
printf 'Cut %s is available.\n' "$fruit"
else
printf 'There is no cut %s.\n' "$fruit"
fi
done
Sample output:
Cut APPLE is available.
There is no cut BANANA.
Cut RED BANANA is available.
There is no cut MANGO.
Here is a little game to play with associative arrays keys and boolean checks:
#!/usr/bin/env bash
print_basket() {
# Print remaining keys (content of basket)
for fruit in "${!basket[#]}"; do
printf '%d %s\n' "${basket[$fruit]}" "${fruit,,}"
done
}
cat <<'EOF'
--:{# Guess the fruits in my basket #}:--
EOF
declare -a all_fruits=(
'apple' 'banana' 'cherry' 'lemon' 'mango'
'orange' 'peach' 'plum' 'red banana' 'sweat chestnut')
# Number of fruits in the basked
selection_size=3
declare -a fruits_selection
mapfile -t fruits_selection < <(
# Shuffle a selection of fruits
printf '%s\n' "${all_fruits[#]}" | shuf -n "$selection_size"
)
declare -A basket
# Set selected fruits into basked
for fruit in "${fruits_selection[#]}"; do
# Uppercase the key for case insensitive match later
basket[${fruit^^}]=$((RANDOM % 9 + 1))
done
# Cheating
declare -p basket
max_tries=3
tries_left=$max_tries
guessed=0
while [ $guessed -lt $selection_size ] && [ $tries_left -gt 0 ]; do
printf 'Enter the name of a fruit: '
read -r fruit
# Uppercase fruit for case insensitive match
fruit="${fruit^^}"
if [[ -v basket[$fruit] ]]; then
printf 'Yes, there is %d %s in my basket!\n' \
"${basket[$fruit]}" "${fruit,,}"
guessed=$((guessed + 1))
# Remove fruit from basket
unset "basket[$fruit]"
else
tries_left=$((tries_left - 1))
printf 'No, there is no %s in my basket!\nTries left: %d\n' \
"${fruit,,}" "$tries_left"
fi
done
case $guessed in
"$selection_size")
printf '* * * Congratulations, you guessed all fruilts! * * *\n'
;;
0)
printf 'Sorry you could not guess any of the fruits!\nBasket contained:\n'
print_basket
;;
*)
printf 'Nice! You guessed %d fruits out of %s!\nUnguessed fruits:\n' \
"$guessed" "$selection_size"
print_basket
;;
esac

Related

Setting changing env variable from associative array [duplicate]

I'm attempting to read an input file line by line which contains fields delimited by periods.
I want to put them into an array of arrays so I can loop through them later on. The input appears to be ok, but 'pushing' that onto the array (inData) doesn't appear to be working.
The code goes :
Input file:
GSDB.GOSALESDW_DIST_INVENTORY_FACT.MONTH_KEY
GSDB.GOSALESDW_DIST_INVENTORY_FACT.ORGANIZATION_KEY
infile=${1}
OIFS=$IFS
IFS=":"
cat ${infile} | while read line
do
line=${line//\./:}
inarray=(${line})
# echo ${inarray[#]}
# echo ${#inarray[#]}
# echo ${inarray[0]}
# echo ${inarray[1]}
# echo ${inarray[2]}
inData=("${inData[#]}" "${inarray[#]}")
done
IFS=$OIFS
echo ${#inData[#]}
for ((i = 0; i < ${#inData[#]}; i++))
do
echo $i
for ((j = 0; j < ${#inData[$i][#]}; j++))
do
echo ${inData[$i][$j]}
done
done
Field nest box in bash but it can not circumvent see the example.
#!/bin/bash
# requires bash 4 or later; on macOS, /bin/bash is version 3.x,
# so need to install bash 4 or 5 using e.g. https://brew.sh
declare -a pages
pages[0]='domain.de;de;https'
pages[1]='domain.fr;fr;http'
for page in "${pages[#]}"
do
# turn e.g. 'domain.de;de;https' into
# array ['domain.de', 'de', 'https']
IFS=";" read -r -a arr <<< "${page}"
site="${arr[0]}"
lang="${arr[1]}"
prot="${arr[2]}"
echo "site : ${site}"
echo "lang : ${lang}"
echo "prot : ${prot}"
echo
done
Bash has no support for multidimensional arrays. Try
array=(a b c d)
echo ${array[1]}
echo ${array[1][3]}
echo ${array[1]exit}
For tricks how to simulate them, see Advanced Bash Scripting Guide.
Knowing that you can split string into "array". You could creat a list of lists. Like for example a list of databases in DB servers.
dbServersList=('db001:app001,app002,app003' 'db002:app004,app005' 'dbcentral:central')
# Loop over DB servers
for someDbServer in ${dbServersList[#]}
do
# delete previous array/list (this is crucial!)
unset dbNamesList
# split sub-list if available
if [[ $someDbServer == *":"* ]]
then
# split server name from sub-list
tmpServerArray=(${someDbServer//:/ })
someDbServer=${tmpServerArray[0]}
dbNamesList=${tmpServerArray[1]}
# make array from simple string
dbNamesList=(${dbNamesList//,/ })
fi
# Info
echo -e "\n----\n$someDbServer\n--"
# Loop over databases
for someDB in ${dbNamesList[#]}
do
echo $someDB
done
done
Output of above would be:
----
db001
--
app001
app002
app003
----
db002
--
app004
app005
----
dbcentral
--
central
I struggled with this but found an uncomfortable compromise. In general, when faced with a problem whose solution involves using data structures in Bash, you should switch to another language like Python. Ignoring that advice and moving right along:
My use cases usually involve lists of lists (or arrays of arrays) and looping over them. You usually don't want to nest much deeper than that. Also, most of the arrays are strings that may or may not contain spaces, but usually don't contain special characters. This allows me to use not-to-confusing syntax to express the outer array and then use normal bash processing on the strings to get a second list or array. You will need to pay attention to your IFS delimiter, obvi.
Thus, associative arrays can give me a way to create a list of lists like:
declare -A JOB_LIST=(
[job1] = "a set of arguments"
[job2] = "another different list"
...
)
This allows you to iterate over both arrays like:
for job in "${!JOB_LIST[#]}"; do
/bin/jobrun ${job[#]}
done
Ah, except that the output of the keys list (using the magical ${!...}) means that you will not traverse your list in order. Therefore, one more necessary hack is to sort the order of the keys, if that is important to you. The sort order is up to you; I find it convenient to use alphanumerical sorting and resorting to aajob1 bbjob3 ccjob6 is perfectly acceptable.
Therefore
declare -A JOB_LIST=(
[aajob1] = "a set of arguments"
[bbjob2] = "another different list"
...
)
sorted=($(printf '%s\n' "${!JOB_LIST[#]}"| /bin/sort))
for job in "${sorted[#]}"; do
for args in "${job[#]}"; do
echo "Do something with ${arg} in ${job}"
done
done
I use Associative Arrays and use :: in the key to denote depth.
The :: can also be used to embed attributes, but that is another subject,...
declare -A __myArrayOfArray=([Array1::Var1]="Assignment" [Array2::Var1]="Assignment")
An Array under Array1
__myArrayOfArray[Array1::SubArray1::Var1]="Assignment"
The entries in any array can be retrieved (in order ...) by ...
local __sortedKeys=`echo ${!__myArrayOfArray[#]} | xargs -n1 | sort -u | xargs`
for __key in ${__sortedKeys}; do
#
# show all properties in the Subordinate Profile "Array1::SubArray1::"
if [[ ${__key} =~ ^Array1::SubArray1:: ]]; then
__property=${__key##Array1::SubArray1::}
if [[ ${__property} =~ :: ]]; then
echo "Property ${__property%%:*} is a Subordinate array"
else
echo "Property ${__property} is set to: ${__myArrayOfArray[${__key}]}"
fi
fi
done
THE list of subordinate "Profiles" can be derived by:
declare -A __subordinateProfiles=()
local __profile
local __key
for __key in "${!__myArrayOfArray[#]}"; do
if [[ $__key =~ :: ]]; then
local __property=${__key##*:}
__profile=${__key%%:*}
__subordinateProfiles[${__profile}]=1
fi
done
A bash array of arrays is possible, if you convert and store each array as a string using declare -p (see my function stringify). This will properly handle spaces and any other problem characters in your arrays. When you extract an array, use function unstringify to rebuild the array. This script demonstrates an array of arrays:
#!/bin/bash
# BASH array of arrays demo
# Convert an array to a string that can be used to reform
# the array as a new variable. This allows functions to
# return arrays as strings. Works for arrays and associative
# arrays. Spaces and odd characters are all handled by bash
# declare.
# Usage: stringify variableName
# variableName - Name of the array variable e.g. "myArray",
# NOT the array contents.
# Returns (prints) the stringified version of the array.
# Examples. Use declare to make an array:
# declare -a myArray=( "O'Neal, Dan" "Kim, Mary Ann" )
# (Or to make a local variable replace declare with local.)
# Stringify myArray:
# stringifiedArray="$(stringify myArray)"
# Reform the array with any name like reformedArray:
# eval "$(unstringify reformedArray "$stringifiedArray")"
# To stringify an argument list "$#", first create the array
# with a name: declare -a myArgs=( "$#" )
stringify() {
declare -p $1
}
# Reform an array from a stringified array. Actually this prints
# the declare command to form the new array. You need to call
# eval with the result to make the array.
# Usage: eval "$(unstringify newArrayName stringifiedArray [local])"
# Adding the optional "local" will create a local variable
# (uses local instead of declare).
# Example to make array variable named reformedArray from
# stringifiedArray:
# eval "$(unstringify reformedArray "$stringifiedArray")"
unstringify() {
local cmd="declare"
[ -n "$3" ] && cmd="$3"
# This RE pattern extracts 2 things:
# 1: the array type, should be "-a" or "-A"
# 2: stringified contents of the array
# and skips "declare" and the original variable name.
local declareRE='^declare ([^ ]+) [^=]+=(.*)$'
if [[ "$2" =~ $declareRE ]]
then
printf '%s %s %s=%s\n' "$cmd" "${BASH_REMATCH[1]}" "$1" "${BASH_REMATCH[2]}"
else
echo "*** unstringify failed, invalid stringified array:" 1>&2
printf '%s\n' "$2" 1>&2
return 1
fi
}
# array of arrays demo
declare -a array # the array holding the arrays
declare -a row1=( "this is" "row 1" )
declare -a row2=( "row 2" "has problem chars" '!##$%^*(*()-_=+[{]}"|\:;,.<.>?/' )
declare -a row3=( "$#" ) # row3 is the arguments to the script
# Fill the array with each row converted to a string.
# stringify needs the NAME OF THE VARIABLE, not the variable itself
array[0]="$(stringify row1)"
array[1]="$(stringify row2)"
array[2]="$(stringify row3)"
# Print array contents
for row in "${array[#]}"
do
echo "Expanding stringified row: $row"
# Reform the row as the array thisRow
eval "$(unstringify thisRow "$row")"
echo "Row values:"
for val in "${thisRow[#]}"
do
echo " '$val'"
done
done
You could make use of (de)referencing arrays like in this script:
#!/bin/bash
OFS=$IFS # store field separator
IFS="${2: }" # define field separator
file=$1 # input file name
unset a # reference to line array
unset i j # index
unset m n # dimension
### input
i=0
while read line
do
a=A$i
unset $a
declare -a $a='($line)'
i=$((i+1))
done < $file
# store number of lines
m=$i
### output
for ((i=0; i < $m; i++))
do
a=A$i
# get line size
# double escape '\\' for sub shell '``' and 'echo'
n=`eval echo \\${#$a[#]}`
for (( j = 0; j < $n; j++))
do
# get field value
f=`eval echo \\${$a[$j]}`
# do something
echo "line $((i+1)) field $((j+1)) = '$f'"
done
done
IFS=$OFS
Credit to https://unix.stackexchange.com/questions/199348/dynamically-create-array-in-bash-with-variables-as-array-name

Substition in Bash and associative array

I want to dynamically declare and unset associative array, but arrays drive me crazy and they do have the best driving licence. :-(
names=( Charlie Snoopy Linux Marcia )
intestines=$(printf "%s\n" ${names[#]} | awk '{ print "["$1"]="FNR }' | tr "\n" " ")
echo $intestines # ok: [Charlie]=1 [Snoopy]=2 [Linux]=3 [Marcia]=4
unset namesAssociative
declare -A namesAssociative=( [Charlie]=1 [Snoopy]=2 [Linux]=3 [Marcia]=4 ) # works ok
echo ${namesAssociative[Linux]} # OK: 3
But:
unset namesAssociative
declare -A namesAssociative=( $intestines ) # error
exec "declare -A namesAssociative=( $intestines )" # error
declare -A namesAssociative=( $(printf "%s\n" ${names[#]} | awk '{ print "["$1"]="FNR }' | tr "\n" " ") ) # error
etc...
I guess God punishes me that I have not written that in Python from the very begining... :-)
This works as you expect and is made safe using the %q format indicator for the associative array's keys.
#!/usr/bin/env bash
names=( Charlie Snoopy Linux Marcia )
# shellcheck disable=SC2155 # Intended dynamic declaration
declare -A namesAssociative="($(
for i in "${!names[#]}"; do
printf '[%q]=%d ' "${names[i]}" $((i + 1))
done
))"
declare -p namesAssociative
or Alternatively if your names array is not sparse:
declare -A namesAssociative="($(
i=1
for k in "${names[#]}"; do
printf '[%q]=%d ' "$k" $((i++))
done
))"
Easier demo, putting associative array name arr in list:
assoc_list="arr[first]=1 arr[second]=2 arr[third]=3"
unset arr
declare -A arr
eval $assoc_list
echo ${arr[second]}
2
Another demo setting associative array name arr later:
assoc_list="[first]=1 [second]=2 [third]=3"
# set associate array name as variable with value arr
assoc_arr_name=arr
# create assoc_array_list from assoc_list
assoc_array_list=${assoc_list//[/$assoc_arr_name[}
echo $assoc_array_list
arr[first]=1 arr[second]=2 arr[third]=3
unset $assoc_arr_name
declare -A $assoc_arr_name
eval $assoc_array_list
echo ${arr[second]}
2
In more general way:
keys_arr=(first second third)
values_arr=(1 2 3)
map_name=assoc_arr
unset $map_name
declare -A $map_name
for ((i = 0 ; i < ${#keys_arr[#]} ; i++)); do
eval $map_name[${keys_arr[i]}]=${values_arr[i]};
done
echo ${assoc_arr[second]}
2
You are insisting too strongly on creating the array from a dynamic string literal in one go, which, depending on various peculiarities of the order of expansion, may require an eval or the like.
Maybe don’t push it that hard and use a for-cycle instead:
declare -a names=( Charlie Snoopy Linux Marcia )
declare -Ai indices1 indices2 # to show two different options
declare -i index
for index in "${!names[#]}"; do
indices1["${names[index]}"]=$((index + 1)) # option 1
indices2+=(["${names[index]}"]=$((index + 1))) # option 2
done
((${#indices1[#]} == ${#indices2[#]})) || echo heck blah
for name in "${!indices1[#]}"; do
echo "${name}: $((indices1["${name}"])) $((indices2["${name}"]))"
done

Reading a particular Digit from a given number in shell [duplicate]

I have a string in a Bash shell script that I want to split into an array of characters, not based on a delimiter but just one character per array index. How can I do this? Ideally it would not use any external programs. Let me rephrase that. My goal is portability, so things like sed that are likely to be on any POSIX compatible system are fine.
Try
echo "abcdefg" | fold -w1
Edit: Added a more elegant solution suggested in comments.
echo "abcdefg" | grep -o .
You can access each letter individually already without an array conversion:
$ foo="bar"
$ echo ${foo:0:1}
b
$ echo ${foo:1:1}
a
$ echo ${foo:2:1}
r
If that's not enough, you could use something like this:
$ bar=($(echo $foo|sed 's/\(.\)/\1 /g'))
$ echo ${bar[1]}
a
If you can't even use sed or something like that, you can use the first technique above combined with a while loop using the original string's length (${#foo}) to build the array.
Warning: the code below does not work if the string contains whitespace. I think Vaughn Cato's answer has a better chance at surviving with special chars.
thing=($(i=0; while [ $i -lt ${#foo} ] ; do echo ${foo:$i:1} ; i=$((i+1)) ; done))
As an alternative to iterating over 0 .. ${#string}-1 with a for/while loop, there are two other ways I can think of to do this with only bash: using =~ and using printf. (There's a third possibility using eval and a {..} sequence expression, but this lacks clarity.)
With the correct environment and NLS enabled in bash these will work with non-ASCII as hoped, removing potential sources of failure with older system tools such as sed, if that's a concern. These will work from bash-3.0 (released 2005).
Using =~ and regular expressions, converting a string to an array in a single expression:
string="wonkabars"
[[ "$string" =~ ${string//?/(.)} ]] # splits into array
printf "%s\n" "${BASH_REMATCH[#]:1}" # loop free: reuse fmtstr
declare -a arr=( "${BASH_REMATCH[#]:1}" ) # copy array for later
The way this works is to perform an expansion of string which substitutes each single character for (.), then match this generated regular expression with grouping to capture each individual character into BASH_REMATCH[]. Index 0 is set to the entire string, since that special array is read-only you cannot remove it, note the :1 when the array is expanded to skip over index 0, if needed.
Some quick testing for non-trivial strings (>64 chars) shows this method is substantially faster than one using bash string and array operations.
The above will work with strings containing newlines, =~ supports POSIX ERE where . matches anything except NUL by default, i.e. the regex is compiled without REG_NEWLINE. (The behaviour of POSIX text processing utilities is allowed to be different by default in this respect, and usually is.)
Second option, using printf:
string="wonkabars"
ii=0
while printf "%s%n" "${string:ii++:1}" xx; do
((xx)) && printf "\n" || break
done
This loop increments index ii to print one character at a time, and breaks out when there are no characters left. This would be even simpler if the bash printf returned the number of character printed (as in C) rather than an error status, instead the number of characters printed is captured in xx using %n. (This works at least back as far as bash-2.05b.)
With bash-3.1 and printf -v var you have slightly more flexibility, and can avoid falling off the end of the string should you be doing something other than printing the characters, e.g. to create an array:
declare -a arr
ii=0
while printf -v cc "%s%n" "${string:(ii++):1}" xx; do
((xx)) && arr+=("$cc") || break
done
If your string is stored in variable x, this produces an array y with the individual characters:
i=0
while [ $i -lt ${#x} ]; do y[$i]=${x:$i:1}; i=$((i+1));done
The most simple, complete and elegant solution:
$ read -a ARRAY <<< $(echo "abcdefg" | sed 's/./& /g')
and test
$ echo ${ARRAY[0]}
a
$ echo ${ARRAY[1]}
b
Explanation: read -a reads the stdin as an array and assigns it to the variable ARRAY treating spaces as delimiter for each array item.
The evaluation of echoing the string to sed just add needed spaces between each character.
We are using Here String (<<<) to feed the stdin of the read command.
I have found that the following works the best:
array=( `echo string | grep -o . ` )
(note the backticks)
then if you do: echo ${array[#]} ,
you get: s t r i n g
or: echo ${array[2]} ,
you get: r
Pure Bash solution with no loop:
#!/usr/bin/env bash
str='The quick brown fox jumps over a lazy dog.'
# Need extglob for the replacement pattern
shopt -s extglob
# Split string characters into array (skip first record)
# Character 037 is the octal representation of ASCII Record Separator
# so it can capture all other characters in the string, including spaces.
IFS= mapfile -s1 -t -d $'\37' array <<<"${str//?()/$'\37'}"
# Strip out captured trailing newline of here-string in last record
array[-1]="${array[-1]%?}"
# Debug print array
declare -p array
string=hello123
for i in $(seq 0 ${#string})
do array[$i]=${string:$i:1}
done
echo "zero element of array is [${array[0]}]"
echo "entire array is [${array[#]}]"
The zero element of array is [h]. The entire array is [h e l l o 1 2 3 ].
Yet another on :), the stated question simply says 'Split string into character array' and don't say much about the state of the receiving array, and don't say much about special chars like and control chars.
My assumption is that if I want to split a string into an array of chars I want the receiving array containing just that string and no left over from previous runs, yet preserve any special chars.
For instance the proposed solution family like
for (( i=0 ; i < ${#x} ; i++ )); do y[i]=${x:i:1}; done
Have left overs in the target array.
$ y=(1 2 3 4 5 6 7 8)
$ x=abc
$ for (( i=0 ; i < ${#x} ; i++ )); do y[i]=${x:i:1}; done
$ printf '%s ' "${y[#]}"
a b c 4 5 6 7 8
Beside writing the long line each time we want to split a problem, so why not hide all this into a function we can keep is a package source file, with a API like
s2a "Long string" ArrayName
I got this one that seems to do the job.
$ s2a()
> { [ "$2" ] && typeset -n __=$2 && unset $2;
> [ "$1" ] && __+=("${1:0:1}") && s2a "${1:1}"
> }
$ a=(1 2 3 4 5 6 7 8 9 0) ; printf '%s ' "${a[#]}"
1 2 3 4 5 6 7 8 9 0
$ s2a "Split It" a ; printf '%s ' "${a[#]}"
S p l i t I t
If the text can contain spaces:
eval a=( $(echo "this is a test" | sed "s/\(.\)/'\1' /g") )
$ echo hello | awk NF=NF FS=
h e l l o
Or
$ echo hello | awk '$0=RT' RS=[[:alnum:]]
h
e
l
l
o
I know this is a "bash" question, but please let me show you the perfect solution in zsh, a shell very popular these days:
string='this is a string'
string_array=(${(s::)string}) #Parameter expansion. And that's it!
print ${(t)string_array} -> type array
print $#string_array -> 16 items
This is an old post/thread but with a new feature of bash v5.2+ using the shell option patsub_replacement and the =~ operator for regex. More or less same with #mr.spuratic post/answer.
str='There can be only one, the Highlander.'
regexp="${str//?/(&)}"
[[ "$str" =~ $regexp ]] &&
printf '%s\n' "${BASH_REMATCH[#]:1}"
Or by just: (which includes the whole string at index 0)
declare -p BASH_REMATCH
If that is not desired, one can remove the value of the first index (index 0), with
unset -v 'BASH_REMATCH[0]'
instead of using printf or echo to print the value of the array BASH_REMATCH
One can check/see the value of the variable "$regexp" with either
declare -p regexp
Output
declare -- regexp="(T)(h)(e)(r)(e)( )(c)(a)(n)( )(b)(e)( )(o)(n)(l)(y)( )(o)(n)(e)(,)( )(t)(h)(e)( )(H)(i)(g)(h)(l)(a)(n)(d)(e)(r)(.)"
or
echo "$regexp"
Using it in a script, one might want to test if the shopt is enabled or not, although the manual says it is on/enabled by default.
Something like.
if ! shopt -q patsub_replacement; then
shopt -s patsub_replacement
fi
But yeah, check the bash version too! If you're not sure which version of bash is in use.
if ! ((BASH_VERSINFO[0] >= 5 && BASH_VERSINFO[1] >= 2)); then
printf 'No dice! bash version 5.2+ is required!\n' >&2
exit 1
fi
Space can be excluded from regexp variable, change it from
regexp="${str//?/(&)}"
To
regexp="${str//[! ]/(&)}"
and the output is:
declare -- regexp="(T)(h)(e)(r)(e) (c)(a)(n) (b)(e) (o)(n)(l)(y) (o)(n)(e) (t)(h)(e) (H)(i)(g)(h)(l)(a)(n)(d)(e)(r)(.)"
Maybe not as efficient as the other post/answer but it is still a solution/option.
If you want to store this in an array, you can do this:
string=foo
unset chars
declare -a chars
while read -N 1
do
chars[${#chars[#]}]="$REPLY"
done <<<"$string"x
unset chars[$((${#chars[#]} - 1))]
unset chars[$((${#chars[#]} - 1))]
echo "Array: ${chars[#]}"
Array: f o o
echo "Array length: ${#chars[#]}"
Array length: 3
The final x is necessary to handle the fact that a newline is appended after $string if it doesn't contain one.
If you want to use NUL-separated characters, you can try this:
echo -n "$string" | while read -N 1
do
printf %s "$REPLY"
printf '\0'
done
AWK is quite convenient:
a='123'; echo $a | awk 'BEGIN{FS="";OFS=" "} {print $1,$2,$3}'
where FS and OFS is delimiter for read-in and print-out
For those who landed here searching how to do this in fish:
We can use the builtin string command (since v2.3.0) for string manipulation.
↪ string split '' abc
a
b
c
The output is a list, so array operations will work.
↪ for c in (string split '' abc)
echo char is $c
end
char is a
char is b
char is c
Here's a more complex example iterating over the string with an index.
↪ set --local chars (string split '' abc)
for i in (seq (count $chars))
echo $i: $chars[$i]
end
1: a
2: b
3: c
zsh solution: To put the scalar string variable into arr, which will be an array:
arr=(${(ps::)string})
If you also need support for strings with newlines, you can do:
str2arr(){ local string="$1"; mapfile -d $'\0' Chars < <(for i in $(seq 0 $((${#string}-1))); do printf '%s\u0000' "${string:$i:1}"; done); printf '%s' "(${Chars[*]#Q})" ;}
string=$(printf '%b' "apa\nbepa")
declare -a MyString=$(str2arr "$string")
declare -p MyString
# prints declare -a MyString=([0]="a" [1]="p" [2]="a" [3]=$'\n' [4]="b" [5]="e" [6]="p" [7]="a")
As a response to Alexandro de Oliveira, I think the following is more elegant or at least more intuitive:
while read -r -n1 c ; do arr+=("$c") ; done <<<"hejsan"
declare -r some_string='abcdefghijklmnopqrstuvwxyz'
declare -a some_array
declare -i idx
for ((idx = 0; idx < ${#some_string}; ++idx)); do
some_array+=("${some_string:idx:1}")
done
for idx in "${!some_array[#]}"; do
echo "$((idx)): ${some_array[idx]}"
done
Pure bash, no loop.
Another solution, similar to/adapted from Léa Gris' solution, but using read -a instead of readarray/mapfile :
#!/usr/bin/env bash
str='azerty'
# Need extglob for the replacement pattern
shopt -s extglob
# Split string characters into array
# ${str//?()/$'\x1F'} replace each character "c" with "^_c".
# ^_ (Control-_, 0x1f) is Unit Separator (US), you can choose another
# character.
IFS=$'\x1F' read -ra array <<< "${str//?()/$'\x1F'}"
# now, array[0] contains an empty string and the rest of array (starting
# from index 1) contains the original string characters :
declare -p array
# Or, if you prefer to keep the array "clean", you can delete
# the first element and pack the array :
unset array[0]
array=("${array[#]}")
declare -p array
However, I prefer the shorter (and easier to understand for me), where we remove the initial 0x1f before assigning the array :
#!/usr/bin/env bash
str='azerty'
shopt -s extglob
tmp="${str//?()/$'\x1F'}" # same as code above
tmp=${tmp#$'\x1F'} # remove initial 0x1f
IFS=$'\x1F' read -ra array <<< "$tmp" # assign array
declare -p array # verification

Bash to split string and numbering it just like Python enumerate function

I found interesting way to split string using tr or IFS
https://linuxhandbook.com/bash-split-string/
#!/bin/bash
#
# Script to split a string based on the delimiter
my_string="One;Two;Three"
my_array=($(echo $my_string | tr ";" "\n"))
#Print the split string
for i in "${my_array[#]}"
do
echo $i
done
Output
One
Two
Three
Based on this code, would be be possible to put a number in front of the string by using Bash?
In Python, there is enumerate function to accomplish this.
number = ['One', 'Two', 'Three']
for i,j in enumerate(number, 1):
print(f'{i} - {j}')
Output
1 - One
2 - Two
3 - Three
I belive there should be similar tricks can be done in Bash Shell probably with awk or sed, but I just can't think the solution for now.
I think you can just add something like count=$(($count+1))
#!/bin/bash
#
# Script to split a string based on the delimiter
my_string="One;Two;Three"
my_array=($(echo $my_string | tr ";" "\n"))
#Print the split string
count=0
for i in "${my_array[#]}"
do
count=$(($count+1))
echo $count - $i
done
This is a slightly modified version of #anubhava's answer.
y_string="One;Two;Three"
IFS=';' read -ra my_array <<< "$my_string"
# ${!array_name[#]} returns the indices/keys of the array
for i in "${!my_array[#]}"
do
echo "$((i+1)) - ${my_array[i]}"
done
From the bash manual,
It is possible to obtain the keys (indices) of an array as well as the values. ${!name[#]} and ${!name[*]} expand to the indices assigned in array variable name.
I saw you posted a post earlier today, sorry I failed to upload the code but still hope this could help you
my_string="AA-BBB"
IFS='-' read -ra my_array <<< "$my_string"
len=${#my_array[#]}
for (( i=0; i<$len; i++ )); do
up=$(($i % 2))
#echo $up
if [ $up -eq 0 ]
then
echo ${my_array[i]} = '"Country name"'
elif [ $up -eq 1 ]
then
echo ${my_array[i]} = '"City name"'
fi
done
Here is a standard bash way of doing this:
my_string="One;Two;Three"
IFS=';' read -ra my_array <<< "$my_string"
# builds my_array='([0]="One" [1]="Two" [2]="Three")'
# loop through array and print index+1 with element
# ${#my_array[#]} is length of the array
for ((i=0; i<${#my_array[#]}; i++)); do
printf '%d: %s\n' $((i+1)) "${my_array[i]}"
done
1: One
2: Two
3: Three

in bash,now i have two strings,one is 'a b c' while another is '1 2 3' any good way to combine them to 'a=1 b=2 c=3' I

in bash,now i have two strings,one is 'a b c' while another is '1 2 3'
any good way to combine them to 'a=1 b=2 c=3'
I tried string to array and combined them.but if i don't know the IFS?
IFS=' ' read -r -a array1 <<< "$upvote_count"
IFS=' ' read -r -a array0 <<< "$qids"
tLen=${#array[#]}
for (( i=0; i<${tLen}; i++ ));
do
echo "${array0[$i]}"" ""${array1[$i]}">>a.txt
done
You can create arrays from each string[1] and then use eval to create the variables with names from string 1 and values from string 2 by looping over each array element and evaluating array1[i]=array2[i] (pseudocode). A short script would look like the following:
#!/bin/bash
v1="a b c" ## original string variables
v2="1 2 3"
ar1=( $(echo $v1) ) ## create arrays 1 & 2 from strings
ar2=( $(echo $v2) )
for ((i=0; i<${#ar1[#]}; i++)); do
eval "${ar1[i]}=${ar2[i]}" ## eval to create variables
done ## (be careful with eval)
printf "a=%s\nb=%s\nc=%s\n" $a $b $c ## confirm
Output
$ bash evalabc.sh
a=1
b=2
c=3
You would want to add validations that the you have the same number of elements in each array, that the elements of the first don't begin with numbers, etc.. and that they do not contain anything that would be harmful when you run eval!
As noted in the comment of the script, take great care in using eval (or avoid it altogether) because it will do exactly what you tell it to do. You would not want to have, e.g. a=sudo rm, b="-rf", c=/* and then eval "$a $b $c" -- very bad things can happen.
Footnotes:
[1] (adjust IFS as needed - not needed for space separation)
Give this tested version a try:
unset letters; unset figures; IFS=' ' read -r -a letters <<< "a b c" ; \
IFS=' ' read -r -a figures <<< '1 2 3' ; \
for i in "${!letters[#]}" ; do \
printf "%s=%s\n" ${letters[i]} ${figures[i]}; \
done
a=1
b=2
c=3
A general method that works in any Bourne shell, (not just bash): Use a for loop for one list (a b c), match it up with piped input for the second list (1 2 3), produce a string of shell code, and eval that.
eval $(seq 3 | for f in a b c ; do read x ; echo $f=$x ; done) ;
echo $a $b $c
which prints:
1 2 3
eval is needed because variables assigned in a loop are forgotten once the loop is over.
Caution: never let eval execute unknown code. If either list contained unwanted shell code delimiters or commands, further parsing would be necessary, i.e. a prophylactic pipe after '; done'.
Applied to the specific example in the starting question, with two strings, containing space separated lists, we get:
qids="a b c" # our variable names
unset $qids # clear them if need be
upvote_count="1 2 3" # the values to be assigned
# generate code to assign the names list to the values list,
# via a 'for' loop, the index $var_name is for the $qids,
# and assign $var_name to each $upvote_count value which we pipe in.
# Since an assignment can't leave the loop, we 'echo'
# the code for assignment, and 'eval' that code after the loop
# is done.
eval $(
echo "${upvote_count}" |
tr ' ' '\n' |
for var_name in $qids ; do
read value
echo "$var_name=$value"
done
)
# The loop is done, so test if the code worked:
for var_name in $qids
do
echo -n $var_name=
eval echo \$$var_name
done
...which outputs:
a=1
b=2
c=3

Resources