Unix & Linux Stack Exchange
Q&A for users of Linux, FreeBSD and other Unix-like operating systems
Latest Questions
2
votes
1
answers
311
views
How can I take a sub-array in bash of the first N elements of a string array with elements containing spaces?
This question is similar to [this one][1] but it differs from it: Consider this array with string elements which may contain spaces: a@W:$ arr=("eins" "zwei" "eins plus zwei" "vier" "fünf") a@W:$ echo ${#arr[@]} # ok, 5 elements, as expected 5 a@W:$ echo ${arr[3]} # the fourth one, indexing sta...
This question is similar to this one but it differs from it:
Consider this array with string elements which may contain spaces:
a@W:$ arr=("eins" "zwei" "eins plus zwei" "vier" "fünf")
a@W:$ echo ${#arr[@]} # ok, 5 elements, as expected
5
a@W:$ echo ${arr} # the fourth one, indexing starts at 0
vier
a@W:$ echo ${arr} # the third one, which contains two blancs
eins plus zwei
a@W:$ ar2=${arr[@]:0:3} # I only want the firs three of them
a@W:$ echo ${ar2[@]}
eins zwei eins plus zwei
a@W:$ echo ${#ar2[@]} # but they are all glued together into one element
1
a@W:$
**What mus I do to prevent this gluing them all together?** The string containing spaces, "eins plus zwei" shall stay the third element.
Adalbert Hanßen
(303 rep)
Mar 24, 2025, 12:47 PM
• Last activity: Mar 24, 2025, 11:53 PM
0
votes
2
answers
57
views
pipe to uniq from a variable not showing the desired output
I have a pipeline using array jobs and need to change the number of inputs for some steps. I thought about testing `uniq` since the only part changing in my folders are the last four characters (the *hap* part in the example). So, all my paths look something like: ``` /mnt/nvme/user/something1/hap1...
I have a pipeline using array jobs and need to change the number of inputs for some steps. I thought about testing
uniq
since the only part changing in my folders are the last four characters (the *hap* part in the example). So, all my paths look something like:
/mnt/nvme/user/something1/hap1
/mnt/nvme/user/something1/hap2
/mnt/nvme/user/something2/hap1
/mnt/nvme/user/something2/hap2
and what I'm doing is the following:
DIR=( "/mnt/nvme/ungaro/something1/hap1" "/mnt/nvme/ungaro/something1/hap2" "/mnt/nvme/ungaro/something2/hap1" "/mnt/nvme/ungaro/something2/hap2" )
for dir in "${DIR[@]}"; do echo $dir | sed 's#/hap[0-9]##' | uniq; done
But the resulting output always displays all the elements in the variable without collapsing the duplicate rows after removing the *hap* part of each one of them.
Probably I'm missing something, could it be that the for
forces to print all lines anyway. If so, is there a way to attained the desired result in a single line command?
Matteo
(209 rep)
Feb 10, 2025, 04:25 PM
• Last activity: Feb 10, 2025, 05:16 PM
1
votes
1
answers
888
views
Creating and appending to an array, mapfile vs arr+=(input) same thing or am I missing something?
Is there a case where `mapfile` has benefits over `arr+=(input)`? ## Simple examples ### mapfile array name, arr: ```bash mkdir {1,2,3} mapfile -t arr < <(ls) declare -p arr ``` ### output: ```bash declare -a arr=([0])="1" [1]="2" [2]="3") ``` Edit: changed title for below; the body had `y` as the a...
Is there a case where
mapfile
has benefits over arr+=(input)
?
## Simple examples
### mapfile array name, arr:
mkdir {1,2,3}
mapfile -t arr < <(ls)
declare -p arr
### output:
declare -a arr=()="1" ="2" ="3")
Edit:
changed title for below; the body had y
as the array name, but the title had arr
as the name, which this could lead to confusion.
## y+=(input)
IFS=$'\n'
y+=($(ls))
declare -p y
### output:
declare -a y=()="1" ="2" ="3")
---
An advantage to mapfile
is you don't have to worry about word splitting I think.
For the other way you can avoid word splitting by setting IFS=$'\n'
although for this example it's nothing to worry about.
The second example just seems easier to write, anything I'm missing out on?
Nickotine
(554 rep)
Jun 25, 2023, 01:23 AM
• Last activity: Jan 31, 2025, 02:29 PM
0
votes
4
answers
189
views
bash - slice quoted substrings delimited by spaces into array
following example: ``` string=" 'f o o' 'f oo' 'fo o' " array=($string) echo "${array[0]}" ``` outputs: ``` 'f ``` while the expected output is: ``` 'f o o' ``` The only solution I came with is by changing the delimiter to newline (rather than space): ``` IFS=$'\n' string="'f o o' # 'f oo' # 'fo o'...
following example:
string=" 'f o o' 'f oo' 'fo o' "
array=($string)
echo "${array}"
outputs:
'f
while the expected output is:
'f o o'
The only solution I came with is by changing the delimiter to newline (rather than space):
IFS=$'\n'
string="'f o o' # 'f oo' # 'fo o' #"
array=($(echo "$string" | tr '#' '\n'))
echo "${array}"
outputs:
'f o o'
which outputs the expected result, however this requires to set a custom delimiter in string and it's will invoke a new subprocess on each call. I would like to see a better solution than this, thanks you!
**Edit:**
The above example string should be enough, but since some has asked what the file contents looks like, it's something similar to this:
'magic1' 'some text' 'another simple text' 'some t£xt'
'magic2' 'another line with simple text' 'foo bar' 'yeah another substring'
'magic3' 'so@m{e$%& te+=*&' 'another substring' 'here is the substring'
'magic4' 'why not another line !' 'yeah I like that' 'the substring yeah !!!'
Even more details (as some users requested):
there is a custom function which uses grep
to return the line from the above file by using a magic substring
.
for example my find function similar to this:
_find()
{
string="$(grep "$1" "$file")"
}
then I call it like this:
_find "magic2"
echo "$string"
which outputs:
'magic2' 'another line with simple text' 'foo bar' 'yeah another substring'
P/S: each substring is delimited by space, and each substring are quoted with single quotes. editing the _find
is not possible since it's used by other functions. please see the comments for more.
Zero
(39 rep)
Jan 5, 2025, 12:30 PM
• Last activity: Jan 7, 2025, 12:53 PM
38
votes
15
answers
50619
views
Bash - reverse an array
Is there a simple way to reverse an array? #!/bin/bash array=(1 2 3 4 5 6 7) echo "${array[@]}" so I would get: `7 6 5 4 3 2 1` instead of: `1 2 3 4 5 6 7`
Is there a simple way to reverse an array?
#!/bin/bash
array=(1 2 3 4 5 6 7)
echo "${array[@]}"
so I would get:
7 6 5 4 3 2 1
instead of: 1 2 3 4 5 6 7
nath
(6094 rep)
Dec 25, 2017, 12:53 AM
• Last activity: Dec 24, 2024, 07:05 PM
2
votes
3
answers
449
views
Replace prefix string from lines in a file, and put into a bash array
In the file `groupAfiles.txt` are the lines: file14 file2 file4 file9 I need a way to convert them to remove `file` and add `/dev/loop` and put them all in one line with a space between them. /dev/loop14 /dev/loop2 /dev/loop4 /dev/loop9 Then I need to put this in an array. (but the numbers change) H...
In the file
groupAfiles.txt
are the lines:
file14
file2
file4
file9
I need a way to convert them to remove file
and add /dev/loop
and put them all in one line with a space between them.
/dev/loop14 /dev/loop2 /dev/loop4 /dev/loop9
Then I need to put this in an array.
(but the numbers change)
How do I do this?
user447274
(539 rep)
Nov 4, 2024, 05:37 AM
• Last activity: Nov 4, 2024, 03:37 PM
3
votes
2
answers
1841
views
How to extract and delete contents of a zip archive simultaneously?
I want to download and extract a large zip archive (>180 GB) containing multiple small files of a single file format onto an SSD, but I don't have enough storage for both the zip archive and the extracted contents. I know that it would be possible to extract and delete individual files from an archi...
I want to download and extract a large zip archive (>180 GB) containing multiple small files of a single file format onto an SSD, but I don't have enough storage for both the zip archive and the extracted contents. I know that it would be possible to extract and delete individual files from an archive using the zip command as mentioned in the answers [here](https://unix.stackexchange.com/questions/14120/extract-only-a-specific-file-from-a-zipped-archive-to-a-given-directory) and [here](https://superuser.com/questions/600385/remove-single-file-from-zip-archive-on-linux) . I could also get the names of all the files in an archive using the
-l
command, store the results in an array as mentioned [here](https://www.baeldung.com/linux/reading-output-into-array) , filter out the unnecessary values using the method given [here](https://stackoverflow.com/questions/9762413/bash-array-leave-elements-containing-string) , and iterate over them in BASH as mentioned [here](https://www.cyberciti.biz/faq/bash-for-loop-array/) . So, the final logic would look something like this:
1. List the zip file's contents using -l
and store the filenames in a bash array, using regular expressions to match the single file extension present in the archive.
2. Iterate over the array of filenames and successively extract and delete individual files using the -j -d
and -d
commands.
How feasible is this method in terms of time required, logic complexity, and computational resources? I am worried about the efficiency of deleting and extracting single files, especially with such a large archive. If you have any feedback or comments about this approach, I would love to hear them. Thank you all in advance for your help.
**Edit 1:**
It seems this question has become a bit popular. Just in case anyone is interested, here is a BASH script following the logic I have outlined earlier, with batching for the extraction and deletion of files to reduce the number of operations. I have used DICOM files in this example but this would work for any other file type or for any files whose file names can be described by a regular expression. Here is the code:
#!/bin/bash
# Check if a zip file is provided as an argument
if [ -z "$1" ]; then
echo "Usage: $0 "
exit 1
fi
zipfile=$1
# List the contents of the zip file and store .dcm files in an array
mapfile -t dcm_files < <(unzip -Z1 "$zipfile" | grep '\.dcm$')
# Define the batch size
batch_size=10000
total_files=${#dcm_files[@]}
# Process files in batches
for ((i=0; i
The file would have to be saved with a name like .sh
with a .sh
extension and marked as executable. If the script and archive are in the same folder and the name of the archive is .zip
, the method to run the script would be ./inplace_extractor.sh archive.zip
. Feel free to adjust the batch size or the regular expression or account for any subfolders in your archive.
I tried it with my large archive and the performance was absolutely abysmal while the disk space rapidly shrunk, so I would still recommend going with the approaches suggested in other answers.
Kumaresh Balaji Sundararajan
(51 rep)
Nov 12, 2023, 12:24 PM
• Last activity: Oct 5, 2024, 11:37 AM
2
votes
1
answers
291
views
Why does printing an array with @ using printf in bash only print the first element?
I have an array snapshots=(1 2 3 4) When I run printf "${snapshots[*]}\n" It prints as expected 1 2 3 4 But when I run printf "${snapshots[@]}\n" It just prints 1 without a newline. My understanding is that accessing an array with `@` is supposed to expand the array so each element is on a newline b...
I have an array
snapshots=(1 2 3 4)
When I run
printf "${snapshots[*]}\n"
It prints as expected
1 2 3 4
But when I run
printf "${snapshots[@]}\n"
It just prints
1
without a newline. My understanding is that accessing an array with
@
is supposed to expand the array so each element is on a newline but it does not appear to do this with printf
while it does do this with echo
. Why is this?
geckels1
(173 rep)
Apr 12, 2024, 08:41 PM
• Last activity: Jun 17, 2024, 07:25 AM
0
votes
3
answers
134
views
bash array multiplication using bc
I am trying to multiply array values with values derived from the multiplication of a loop index using bc. #!/bin/bash n=10.0 bw=(1e-3 2.5e-4 1.11e-4 6.25e-5 4.0e-5 2.78e-5 2.04e-5 1.56e-5 1.29e-5 1.23e-5 1.0e-5) for k in {1..11};do a=$(echo "$n * $k" | bc) echo "A is $a" arn=${bw[k-1]} echo "Arn is...
I am trying to multiply array values with values derived from the multiplication of a loop index using bc.
#!/bin/bash
n=10.0
bw=(1e-3 2.5e-4 1.11e-4 6.25e-5 4.0e-5 2.78e-5 2.04e-5 1.56e-5 1.29e-5 1.23e-5 1.0e-5)
for k in {1..11};do
a=$(echo "$n * $k" | bc)
echo "A is $a"
arn=${bw[k-1]}
echo "Arn is $arn"
b=$(echo "$arn * $a" | bc -l)
echo "b is $b"
#echo $a $b
done
I am able to echo the array values by assigning it to a new variable within the loop, but when I use that to multiply using
bc
, I get (standard_in) 1: syntax error
.
I searched for clues and tried some but none helped. The expected output is as follows.
10 1.00E-02
20 5.00E-03
30 3.33E-03
40 2.50E-03
50 2.00E-03
60 1.67E-03
70 1.43E-03
80 1.25E-03
90 1.16E-03
100 1.23E-03
110 1.10E-03
All help is greatly appreciated.
csnl
(35 rep)
Mar 23, 2024, 07:00 PM
• Last activity: Mar 23, 2024, 10:46 PM
17
votes
4
answers
17599
views
Run a command using arguments that come from an array
Suppose I have a graphical program named `app`. Usage example: `app -t 'first tab' -t 'second tab'` opens two 'tabs' in that program. The question is: how can I execute the command (i.e. `app`) from within a `bash` script if the number of arguments can vary? Consider this: #!/bin/bash tabs=( 'first...
Suppose I have a graphical program named
app
. Usage example: app -t 'first tab' -t 'second tab'
opens two 'tabs' in that program.
The question is: how can I execute the command (i.e. app
) from within a bash
script if the number of arguments can vary?
Consider this:
#!/bin/bash
tabs=(
'first tab'
'second tab'
)
# Open the app (starting with some tabs).
app # ... How to get app -t 'first tab' -t 'second tab'
?
I would like the above script to have an effect equivalent to app -t 'first tab' -t 'second tab'
. How can such a bash script be written?
Edit: note that the question is asking about composing command line arguments on the fly using an array of arguments.
Flux
(3238 rep)
Dec 23, 2017, 08:25 AM
• Last activity: Feb 23, 2024, 08:42 AM
1
votes
3
answers
3493
views
Bash: converting a string with both spaces and quotes to an array
I have a function (not created by me) that outputs a series of strings inside of quotes: command “Foo” “FooBar” “Foo Bar” “FooBar/Foo Bar” When I try to assign it to an array (Bash; BSD/Mac), instead of 4 elements, I get 7. For example, for `${array[2]}` I *should* get `“Foo Bar”`, but instead, I ge...
I have a function (not created by me) that outputs a series of strings inside of quotes:
command
“Foo”
“FooBar”
“Foo Bar”
“FooBar/Foo Bar”
When I try to assign it to an array (Bash; BSD/Mac), instead of 4 elements, I get 7. For example, for
${array}
I *should* get “Foo Bar”
, but instead, I get ”Foo
with the next element being Bar”
. Any element *without* the space works correctly (i.e. ${array}
= “Foo”)
**How can assign each of these elements between the quote including the space to an array that the elements are separated by spaces(?) themselves?**
Right now, I am thinking of using sed/awk to “strip” out the quotes, but I think there should be a better and more efficient way.
Currently, I am assigning the output of the command (looks exactly like the output above including the quotes) to a temporary variable then assigning it to an array.
_tempvar=“$(command )”
declare -a _array=(${_tempvar})
Allan
(1090 rep)
Jun 15, 2023, 06:46 PM
• Last activity: Feb 4, 2024, 10:50 PM
0
votes
2
answers
2459
views
Shell script with a for loop and an “array”
How can I use this shell script with for loop and an array. I would like to call create condition for quality gate creation of sonarqube with a for loop. Example: ``` #!/bin/bash --login echo "Creating SonarQube Gateway Condition" QG_ID=$(cat qualitygate.json | jq -r ".id") Gateway="curl -u ${USERNA...
How can I use this shell script with for loop and an array.
I would like to call create condition for quality gate creation of sonarqube with a for loop. Example:
#!/bin/bash --login
echo "Creating SonarQube Gateway Condition"
QG_ID=$(cat qualitygate.json | jq -r ".id")
Gateway="curl -u ${USERNAME}:${PASSWORD} -k -X POST "${SONAR_HOST_URL}/api/qualitygates/create_condition?"
declare -a gateMetrics=("gateId=$QG_ID&metric=coverage&op=LT&error=80\"" "gateId=$QG_ID&metric=duplicated_lines_density&op=GT&error=10\"")
for val in "${gateMetrics[@]}"
do
echo $Gateway$val
done
I want the output as below after running above command
curl -u ${USERNAME}:${PASSWORD} -k -X POST "${SONAR_HOST_URL}/api/qualitygates/create_condition?gateId=$QG_ID&metric=coverage&op=LT&error=80"
user1628
(11 rep)
Jan 25, 2021, 08:09 AM
• Last activity: Dec 17, 2023, 07:03 AM
1
votes
1
answers
365
views
Access values of associative array whose name is passed as argument inside bash function
I've some associative arrays in a bash script which I need to pass to a function in which I need to access the keys and values as well. ``` declare -A gkp=( \ ["arm64"]="ARM-64-bit" \ ["x86"]="Intel-32-bit" \ ) fv() { local entry="$1" echo "keys: ${!gkp[@]}" echo "vals: ${gkp[@]}" local arr="$2[@]"...
I've some associative arrays in a bash script which I need to pass to a function in which I need to access the keys and values as well.
declare -A gkp=( \
["arm64"]="ARM-64-bit" \
["x86"]="Intel-32-bit" \
)
fv()
{
local entry="$1"
echo "keys: ${!gkp[@]}"
echo "vals: ${gkp[@]}"
local arr="$2[@]"
echo -e "\narr entries: ${!arr}"
}
fv $1 gkp
Output for above:
kpi: arm64 x86
kpv: ARM-64-bit Intel-32-bit
arr entries: ARM-64-bit Intel-32-bit
I could get values of array passed to function, but couldn't figure out how to print keys (i.e. "arm64" "x86") in the function.
Please help.
c10
(13 rep)
Sep 30, 2023, 11:21 AM
• Last activity: Sep 30, 2023, 11:56 AM
3
votes
4
answers
3172
views
How do I select an array to loop through from an array of arrays?
``` #!/usr/bin/bash ARGENT=("Nous devons économiser de l'argent." "Je dois économiser de l'argent.") BIENETRE=("Comment vas-tu?" "Tout va bien ?") aoarrs=("${ARGENT}" "${BIENETRE}") select arr in "${aoarrs[@]}"; do for el in "${arr[@]}"; do echo "$el" done break done ``` I want this script...
#!/usr/bin/bash
ARGENT=("Nous devons économiser de l'argent."
"Je dois économiser de l'argent.")
BIENETRE=("Comment vas-tu?" "Tout va bien ?")
aoarrs=("${ARGENT}" "${BIENETRE}")
select arr in "${aoarrs[@]}"; do
for el in "${arr[@]}"; do
echo "$el"
done
break
done
I want this script to print the array names to the user, ARGENT
and BIENETRE
,
so that the user can select one of them. After the user's input the script is meant
to print every element of an array selected. I want to select with select
an array to loop through from an array of arrays (aoarrs
). The reason why I want to use select is because in the real world my array of arrays may have many more than just two arrays in it. How might I accomplish that?
John Smith
(827 rep)
Aug 28, 2023, 09:10 AM
• Last activity: Aug 29, 2023, 05:36 AM
2
votes
1
answers
315
views
What happens if I start a bash array with a big index?
I was trying to create a bash "multidimensional" array, I saw the ideas on using associative arrays, but I thought the simplest way to do it would be the following: ``` for i in 0 1 2 do for j in 0 1 2 do a[$i$j]="something" done done ``` It is easy to set and get values, but the jumps the indexes m...
I was trying to create a bash "multidimensional" array, I saw the ideas on using associative arrays, but I thought the simplest way to do it would be the following:
for i in 0 1 2
do
for j in 0 1 2
do
a[$i$j]="something"
done
done
It is easy to set and get values, but the jumps the indexes might make it terrible for arrays a bit bigger if bash allocates space for the elements from index 00 to 22 sequentially (I mean allocating positions {0,1,2,3,4,...,21,22}), instead of just the elements which were actually set: {00,01,02,10,11,...,21,22}.
This made me wonder, what happens when we start a bash array with an index 'n'? Does it allocate enough space for indexes 0 to n, or does it allocate the nth element sort of individually?
Wellington de Almeida Silva
(23 rep)
Aug 11, 2023, 03:31 PM
• Last activity: Aug 11, 2023, 08:29 PM
0
votes
3
answers
168
views
Unable to append to array using parallel
I can't append to an array when I use `parallel`, no issues using a for loop. ### Parallel example: ```bash append() { arr+=("$1"); } export -f append parallel -j 0 append ::: {1..4} declare -p arr ``` ### Output: ``` -bash: declare: arr: not found ``` ### For loop: ```bash for i in {1..4}; do arr+=...
I can't append to an array when I use
parallel
, no issues using a for loop.
### Parallel example:
append() { arr+=("$1"); }
export -f append
parallel -j 0 append ::: {1..4}
declare -p arr
### Output:
-bash: declare: arr: not found
### For loop:
for i in {1..4}; do arr+=("$i"); done
declare -p arr
### Output:
declare -a arr=(="1" ="2" ="3" ="4")
I thought the first example is a translation of the for loop in functional style, so what's going on?
Nickotine
(554 rep)
Jun 23, 2023, 06:37 PM
• Last activity: Jun 25, 2023, 09:27 AM
4
votes
1
answers
3462
views
Why is "${ARRAY[@]}" expanded into multiple words, when it's quoted?
I don't understand why `"${ARRAY[@]}"` gets expanded to multiple words, when it's quoted (`"..."`)? Take this example: IFS=":" read -ra ARRAY <<< "foo:bar:baz" for e in "${ARRAY[@]}"; do echo $e; done foo bar baz Any other variable that I expand in quotes, say `"${VAR}"`, results in a single word: V...
I don't understand why
"${ARRAY[@]}"
gets expanded to multiple words, when it's quoted ("..."
)?
Take this example:
IFS=":" read -ra ARRAY <<< "foo:bar:baz"
for e in "${ARRAY[@]}"; do echo $e; done
foo
bar
baz
Any other variable that I expand in quotes, say "${VAR}"
, results in a single word:
VAR="foo bar baz"
for a in "${VAR}"; do echo $a; done
foo bar baz
Can anyone explain this to a novice Linux user?
Shuzheng
(4931 rep)
Jan 17, 2020, 05:19 PM
• Last activity: May 4, 2023, 08:48 PM
3
votes
2
answers
688
views
Iterating over array elements with gnu parallel
I have an input file, *names.txt*, with the 1 word per line: apple abble aplle With my bash script I am trying to achieve the following output: apple and apple apple and abble apple and aplle abble and apple abble and abble abble and aplle aplle and apple aplle and abble aplle and aplle Here is my b...
I have an input file, *names.txt*, with the 1 word per line:
apple
abble
aplle
With my bash script I am trying to achieve the following output:
apple and apple
apple and abble
apple and aplle
abble and apple
abble and abble
abble and aplle
aplle and apple
aplle and abble
aplle and aplle
Here is my bash script
#!/usr/bin bash
readarray -t seqcol < names.txt
joiner () {
val1=$1
val2=$2
echo "$val1 and $val2"
}
export -f joiner
parallel -j 20 '
line=($(echo {}))
for word in "${line[@]}"; do
joiner "${line}" "${word}"
done
' ::: "${seqcol[@]}"
but it is only outputting the following 3 lines comparing identical elements from the array
apple and apple
abble and abble
aplle and aplle
I have the script that uses while read line
loop, but it is too slow (my actual datafile is has about 200k lines). That is why I want to use array elements and gnu parallel
at the same to speed the process up.
I have tried different ways of accessing the array elements within the parallel ' '
command (by mainly modifying this loop - for word in "${line[@]}"
, or by supplying the array to parallel
via printf '%s\n' "${seqcol[@]}"
) but they are either leading to errors or output blank lines.
I would appreciate any help!
duda13
(33 rep)
Apr 28, 2023, 03:42 PM
• Last activity: May 2, 2023, 07:37 AM
0
votes
2
answers
457
views
Sort file array list in bash by date and bypass argument limit
So, I have a file array list in a bash shell, and I want to sort all the files in the array by the date modified, starting with the oldest being the first one in the array. However, I don't want to sort and modify the original array, and instead want to have the sorted result be in a different array...
So, I have a file array list in a bash shell, and I want to sort all the files in the array by the date modified, starting with the oldest being the first one in the array. However, I don't want to sort and modify the original array, and instead want to have the sorted result be in a different array. I saw this thread in which I tried the following command, although I modified it since the array was a variable and not a file.
new_array=( $(ls -t $(printf '%s\n' "${array_list[@]}")) )
However, the array is so big that ls reports the argument list is "too long"
Is there another way I can sort the main array by the modified date, starting with the oldest file at the beginning, and save the results to a different array?
Eduardo Perez
(161 rep)
Feb 20, 2023, 06:13 PM
• Last activity: Mar 13, 2023, 06:37 AM
7
votes
1
answers
424
views
Bash 4.4 local readonly array variable scoping: bug?
The following script fails when run with bash 4.4.20(1) ```bash #!/bin/bash bar() { local args=("y") } foo() { local -r args=("x") bar } foo ``` with error `line 3: args: readonly variable` but succeeds when run with bash 4.2.46(2), which makes sense after reading [24.2. Local Variables](https://tld...
The following script fails when run with bash 4.4.20(1)
#!/bin/bash
bar() {
local args=("y")
}
foo() {
local -r args=("x")
bar
}
foo
with error line 3: args: readonly variable
but succeeds when run with bash 4.2.46(2), which makes sense after reading [24.2. Local Variables](https://tldp.org/LDP/abs/html/localvar.html) .
The following script with non-array variables runs without any issues:
#!/bin/bash
bar() {
local args="y"
}
foo() {
local -r args="x"
bar
}
foo
I could not find any [changes](https://git.savannah.gnu.org/cgit/bash.git/tree/CHANGES) that explain the difference between bash 4.2.46(2) and bash 4.4.20(1).
Q: is this a bug in bash 4.4.20(1)? if this is expected behavior then why does the second script not fail?
Dennis
(73 rep)
Feb 22, 2023, 10:52 AM
• Last activity: Feb 22, 2023, 02:02 PM
Showing page 1 of 20 total questions