The Ultimate Bash Array Tutorial with 15 Examples

by Sasikala on June 3, 2010

An array is a variable containing multiple values may be of same type or of different type.  There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. Array index starts with zero.

In this article, let us review 15 various array operations in bash.

This article is part of the on-going Bash Tutorial series. For those who are new to bash scripting, get a jump-start from the Bash Scripting Introduction tutorial.

1. Declaring an Array and Assigning values

In bash, array is created automatically when a variable is used in the format like,

  • name is any name for an array
  • index could be any number or expression that must evaluate to a number greater than or equal to zero.You can declare an explicit array using declare -a arrayname.
$ cat
#! /bin/bash
Unix[1]='Red hat'

echo ${Unix[1]}

Red hat

To access an element from an array use curly brackets like ${name[index]}.

2. Initializing an array during declaration

Instead of initializing an each element of an array separately, you can declare and initialize an array by specifying the list of elements (separated by white space) with in a curly braces.

declare -a arrayname=(element1 element2 element3)

If the elements has the white space character, enclose it with in a quotes.

#! /bin/bash
declare -a Unix=('Debian' 'Red hat' 'Red hat' 'Suse' 'Fedora');

declare -a declares an array and all the elements in the parentheses are the elements of an array.

3. Print the Whole Bash Array

There are different ways to print the whole elements of the array. If the index number is @ or *, all members of an array are referenced. You can traverse through the array elements and print it, using looping statements in bash.

echo ${Unix[@]}

# Add the above echo statement into the
Debian Red hat Ubuntu Suse

Referring to the content of a member variable of an array without providing an index number is the same as referring to the content of the first element, the one referenced with index number zero.

4. Length of the Bash Array

We can get the length of an array using the special parameter called $#.

${#arrayname[@]} gives you the length of the array.

$ cat
declare -a Unix=('Debian' 'Red hat' 'Suse' 'Fedora');
echo ${#Unix[@]} #Number of elements in the array
echo ${#Unix}  #Number of characters in the first element of the array.i.e Debian

5. Length of the nth Element in an Array

${#arrayname[n]} should give the length of the nth element in an array.

#! /bin/bash

Unix[1]='Red hat'

echo ${#Unix[3]} # length of the element located at index 3 i.e Suse


6. Extraction by offset and length for an array

The following example shows the way to extract 2 elements starting from the position 3 from an array called Unix.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[@]:3:2}

Suse Fedora

The above example returns the elements in the 3rd index and fourth index. Index always starts with zero.

7. Extraction with offset and length, for a particular element of an array

To extract only first four elements from an array element . For example, Ubuntu which is located at the second index of an array, you can use offset and length for a particular element of an array.

#! /bin/bash

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Unix[2]:0:4}


The above example extracts the first four characters from the 2nd indexed element of an array.

8. Search and Replace in an array elements

The following example, searches for Ubuntu in an array elements, and replace the same with the word ‘SCO Unix’.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

echo ${Unix[@]/Ubuntu/SCO Unix}

Debian Red hat SCO Unix Suse Fedora UTS OpenLinux

In this example, it replaces the element in the 2nd index ‘Ubuntu’ with ‘SCO Unix’. But this example will not permanently replace the array content.

9. Add an element to an existing Bash Array

The following example shows the way to add an element to the existing array.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=("${Unix[@]}" "AIX" "HP-UX")
echo ${Unix[7]}


In the array called Unix, the elements ‘AIX’ and ‘HP-UX’ are added in 7th and 8th index respectively.

10. Remove an Element from an Array

unset is used to remove an element from an array.unset will have the same effect as assigning null to an element.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');

unset Unix[3]
echo ${Unix[3]}

The above script will just print null which is the value available in the 3rd index. The following example shows one of the way to remove an element completely from an array.

$ cat
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
echo ${Unix[@]}

Debian Red hat Ubuntu Fedora UTS OpenLinux

In this example, ${Unix[@]:0:$pos} will give you 3 elements starting from 0th index i.e 0,1,2 and ${Unix[@]:4} will give the elements from 4th index to the last index. And merge both the above output. This is one of the workaround to remove an element from an array.

11. Remove Bash Array Elements using Patterns

In the search condition you can give the patterns, and stores the remaining element to an another array as shown below.

$ cat
declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
declare -a patter=( ${Unix[@]/Red*/} )
echo ${patter[@]}

$ ./
Debian Ubuntu Suse Fedora

The above example removes the elements which has the patter Red*.

12. Copying an Array

Expand the array elements and store that into a new array as shown below.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
echo ${Linux[@]}

$ ./
Debian Red hat Ubuntu Fedora UTS OpenLinux

13. Concatenation of two Bash Arrays

Expand the elements of the two arrays and assign it to the new array.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');

UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}

$ ./
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh

It prints the array which has the elements of the both the array ‘Unix’ and ‘Shell’, and number of elements of the new array is 14.

14. Deleting an Entire Array

unset is used to delete an entire array.

Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
Shell=('bash' 'csh' 'jsh' 'rsh' 'ksh' 'rc' 'tcsh');

UnixShell=("${Unix[@]}" "${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}

$ ./

After unset an array, its length would be zero as shown above.

15. Load Content of a File into an Array

You can load the content of the file line by line into an array.

#Example file
$ cat logfile

$ cat
filecontent=( `cat "logfile" `)

for t in "${filecontent[@]}"
echo $t
echo "Read file content!"

$ ./
Read file content!

In the above example, each index of an array element has printed through for loop.

Recommended Reading

Bash 101 Hacks, by Ramesh Natarajan. I spend most of my time on Linux environment. So, naturally I’m a huge fan of Bash command line and shell scripting. 15 years back, when I was working on different flavors of *nix, I used to write lot of code on C shell and Korn shell. Later years, when I started working on Linux as system administrator, I pretty much automated every possible task using Bash shell scripting. Based on my Bash experience, I’ve written Bash 101 Hacks eBook that contains 101 practical examples on both Bash command line and shell scripting. If you’ve been thinking about mastering Bash, do yourself a favor and read this book, which will help you take control of your Bash command line and shell scripting.

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

{ 52 comments… read them below or add one }

1 Tanmay Joshi June 3, 2010 at 5:59 am

Good article.

I am new to linux and following your articles very closely.
Just wanted to confirm if the below line as typo in displaying code or the sentence it self
“declare -a declares an array and all the elements in the curly brackets are the elements of an array” – are we using curly brackets or parantheses?


2 Gabriele June 3, 2010 at 6:47 am

Great stuff!!!


3 iambryan June 3, 2010 at 8:52 am

Great examples :)

There is a correction for number 6 though as the OpenLinux array entity is missing the closing single quote which would as you know, throw an error.

Keep up the good work

4 Ramesh Natarajan June 3, 2010 at 10:11 pm

@Tanmay, @Bryan,

Thanks for pointing out the issues. They are fixed now.

5 hari June 3, 2010 at 11:50 pm

best best


6 Chris F.A. Johnson June 4, 2010 at 8:01 am

1. “echo ${Unix[1]}” will not necessarily print element 1 from the array. For example:

$ Unix[1]=” AAA BBB CCC”
$ echo ${Unix[1]}

Leading and trailing whitespace will be lost, and consecutive whitespace will be reduced to a single space.

It should be:

echo “${Unix[1]}”

(Almost all the examples exhibit the same error because the variable reference is not quoted. Whether the error is manifest in the output depends on the contents of the array elements.)

2. “declare -a” is unnecessary

3. “echo ${Unix[@]}” has the same problem as #1

4. More accurately, ${#arrayname[@]} gives you the number of elements in the array.

9. Since bash3, elements can also be appended to an array with “+=”:

Unix+=( “AIX” “HP-UX” )

15. filecontent=( `cat “logfile” `)

More efficient, as it doesn’t require an external command, is:

filecontent=( `< "logfile" `)

(Note: this doesn't read the file line by line; it reads it word by word. Try it on a file with more than one word on a line.)

Since bash4, this can be done even more efficiently with the mapfile builtin:

mapfile -t filecontent < "$logfile"

for t in "${filecontent[@]}"

The loop is not necessary:

printf "%s\n" "${filecontent[@]}"

7 anonymous June 4, 2010 at 10:01 pm

Note that the example will not read the following file into an array (where each line is an element).
This is the first line
This is the second line
This is the final line
To read the file (as lines) into an array do:
array_name=( $(cat filename) )
Note the use of the “{” in this example allows the changing of IFS value without having to save it and restore it.

8 TheFu June 7, 2010 at 9:36 am

I love it! Great examples to display simple use cases.

Sadly, the syntax for arrays in Bash is too complex for me, so I’ll be staying with Perl. How often do you hear that? ;) I’ll probably be back here when perl isn’t allowed on a system for some reason.

9 sbaginov June 9, 2010 at 4:47 am

Simply awsome! Thank you!

10 WaS June 21, 2010 at 3:36 am

When you do:

filecontent=( `cat “logfile” `)

and logfile have one “*” you get a list of archives in your directory, how i can solve it?


11 Chris F.A. Johnson June 21, 2010 at 11:34 am

WaS, when you do that, $logfile will contain just an asterisk (*).

If you want to display that asterisk, you must quote the variable reference or the wildcard will be expanded:

printf “%s\n” “$logfile”


echo “$logfile”

(Always quote variable references unless you have a good reason not to.)

12 Toni Kukul October 8, 2010 at 5:47 pm

To read a file into an array it’s possible to use the readarray or mapfile bash built-ins.
readarray < filename
mapfile < filename

File is read into MAPFILE variable by default.

13 Anderson Venturini February 28, 2012 at 6:08 am

Thanks a lot! Great tutorial! It was very useful! Congrats!

14 ak March 11, 2012 at 10:50 am

Good Examples. Thank you for hard work and clear explanations. Error in number 12: Suse is omitted from the copied array.

15 Vivek May 30, 2012 at 8:07 pm


I need to use cntrC inside my shell script. Here is an example:

run some commands
run some more commands

How can I have my shell script generate cntrC without me typing cnrlC?


16 Chris F.A. Johnson May 31, 2012 at 12:21 pm

Vivek, what do you really want to do?

17 Vivek May 31, 2012 at 8:54 pm

Chris, I need to run a script which has a command which gives a running output. I need to change the argument to that command for example from 1 to 10.
for a in $(seq 1 10)


Now gives a running output.
Now when a=1, the command is running.
I want to send cntrlC to the command so that ends after lets say 100 seconds and starts.


18 Chris F.A. Johnson June 1, 2012 at 12:23 pm

Vivek, what does this have to do with arrays?

for n in {1..10}
: whatever
done &

read && kill “$!”

19 Dennis Dashkevich July 12, 2012 at 3:09 pm

More accurately, the length of the Nth element in an array will give the statement with the N-1 index, i.e. ${#arrayname[N-1]}.

Thanks for the tutorial! It’s really great!

20 h October 29, 2012 at 1:25 am

white space in elements not getting eliminated even though quotes are used

21 Josh January 2, 2013 at 5:41 pm

How about “test to see if value is in array” example?

Below is a small function for achieving this. The search string is the first argument and the rest are the array elements:

containsElement () {
local e
for e in “${@:2}”; do [[ "$e" == "$1" ]] && return 0; done
return 1
A test run of that function could look like:

$ array=(“something to search for” “a string” “test2000″)
$ containsElement “a string” “${array[@]}”
$ echo $?
$ containsElement “blaha” “${array[@]}”
$ echo $?

22 Chris F.A. Johnson January 2, 2013 at 7:23 pm

Josh, there’s no need for a loop:

arraycontains() #@ USAGE: arraycontains STRING ARRAYNAME [IFS]
local string=$1 array=$2 localarray IFS=${3:-:}
eval “localarray=( \”\${$array[@]}\” )”
case “$IFS${localarray[*]}$IFS” in
*”$IFS$string$IFS”*) return ;;
*) return 1 ;;

23 Chris F.A. Johnson January 8, 2013 at 6:41 pm

I have posted a number of functions for manipulating arrays at

24 Angelo January 23, 2013 at 6:32 am

As a historical note: SuSE has a lower-case “u” and the rest upper-case because it originally stood for “Software und System-Entwicklung”, meaning “Software and systems development”. (Ref:

25 Angelo January 23, 2013 at 9:34 am

I’m a fan of clear code that is easy to read, but was curious whether Mr. Johnson’s arraycontains method had an efficiency benefit (in spite of its more obfuscated nature) over Josh’s (which is almost exactly the method I had been using). I can’t get it to work at all. Maybe I’m missing something, but in case I’m not, maybe I can save someone else the wasted effort in going down this same road.

Here is my test of Chris Johnson’s code:

arraycontains() { #@ USAGE: arraycontains STRING ARRAYNAME [IFS]
local string=$1 array=$2 localarray IFS=${3:-:}
eval “localarray=( \”\${$array[@]}\” )”
case “$IFS${localarray[*]}$IFS” in
*”$IFS$string$IFS”*) return ;;
*) return 1 ;;

echo -en “String test 1: ”
one=(“and” “this” “is” “another” “test”)
if arraycontains “something” “${one[@]}”
echo “TRUE, but should be FALSE”
echo “OK”

echo -en “String test 2: ”
if arraycontains “another” “${one[@]}”
echo “OK”
echo “FALSE, but should be TRUE”

echo -en “Numeric test: ”
two=(1 2 3 4 5)
arraycontains “5″ “${two[@]}”
echo $?

echo -en “Quoted-numeric test: ”
three=(“1″ “2″ “3″ “4″ “5″)
arraycontains “6″ “${three[@]}”
echo $?

Here is the output:

$ sh
String test 1: OK
String test 2: FALSE, but should be TRUE
Numeric test: ./ line 4: ${1[@]}: bad substitution
Quoted-numeric test: ./ line 4: ${1[@]}: bad substitution

Besides giving the error message when passed a numeric array, it always returns FALSE (1). On investigation I discovered that the “eval” line is not working; localarray is always blank (so no wonder it always returns false).

I ran this script with BASH 3.00.16 and 4.2.20 and got the same result.

26 Heriel Uronu@udom April 30, 2013 at 7:18 am

yeah… am well and much clear on array in linux command..
wel done stay blessed,

27 x31eq July 17, 2013 at 10:49 am

The second part of Example 10 is especially wrong because of the quoting issue. It means ${Unix[1]} is Red instead of Red hat. The correct way is

Unix=(“${Unix[@]:0:$pos}” “${Unix[@]:$(($pos + 1))}”)

28 Hans November 29, 2013 at 1:49 pm

The best guide on Bash arrays I have ever found!

Thank you.

29 mug896 January 2, 2014 at 6:31 am

To read the file as lines into an array use double quote

fileContents=( “$(cat sunflower.html)” )

for line in “${fileContents[@]}”
echo “$line”

30 Chris F.A. Johnson January 2, 2014 at 11:57 am

That will not read the file line by line; it will read it word by word. All whitespace in the file will act as delimiters.

And you don’t need a loop to print out the array:

printf ‘%s\n’ “${fileContents[@]}”

31 Chris F.A. Johnson January 2, 2014 at 2:10 pm

My mistake, mug896; your code will read the file into a single element of the array. You can see that by:

printf ‘%s\n’ “${fileContents[0]}”

If you had done

fileContents=( $(cat sunflower.html) ) ## no quotes

It would have read each word into a separate element of the array.

32 Offirmo January 9, 2014 at 8:21 am

Very nice, but “iteration on an array” is missing !

33 Erick January 24, 2014 at 5:43 pm

Thanks a lot!

34 Robert Vila February 23, 2014 at 10:57 pm

What do you do when a bash script doesn’t accept arrays?

Error messages:
>>>> “declare: not found”
>>>> “Unix[0]=Debian: not found”

35 Chris F.A. Johnson February 25, 2014 at 3:14 pm

Robert, make sure you are using bash to interpret the script.

36 Robert Mark Bram February 27, 2014 at 8:00 pm

Your second example in “10. Remove an Element from an Array” is wrong because you are not enclosing the array parts in quotes – so ‘Red Hat’ becomes two elements.

Here it is fixed up with proofs.

Fri Feb 28 – 12:53 PM > Unix=(‘Debian’ ‘Red hat’ ‘Ubuntu’ ‘Suse’ ‘Fedora’ ‘UTS’ ‘OpenLinux’);
Fri Feb 28 – 12:53 PM > echo ${#Unix[@]}
Fri Feb 28 – 12:53 PM > pos=3
Fri Feb 28 – 12:53 PM > echo ${Unix[$pos]}
Fri Feb 28 – 12:53 PM > Unix=(“${Unix[@]:0:$pos}” “${Unix[@]:$(($pos + 1))}”)
Fri Feb 28 – 12:53 PM > echo ${Unix[$pos]}
Fri Feb 28 – 12:53 PM > echo ${Unix[@]}
Debian Red hat Ubuntu Fedora UTS OpenLinux
Fri Feb 28 – 12:53 PM > echo ${#Unix[@]}
Fri Feb 28 – 12:53 PM > for index in “${!Unix[@]}” ; do printf “%4d: %s\n” $index “${Unix[$index]}” ; done
0: Debian
1: Red hat
2: Ubuntu
3: Fedora
4: UTS
5: OpenLinux

37 ian fleming March 13, 2014 at 12:37 pm

An alternate, perhaps simpler, method for removing an element, is to reassign Unix (making sure we include the quotes, as per previous post) from the remaining elements in the array (after unsetting):
unset Unix[2]
Unix=( “${Unix[@]” )

—– $ Unix=(‘Debian’ ‘Red Hat’ ‘Ubuntu’ ‘SuSE’);
—– $ echo “len: ${#Unix[@]}”; for ((i=0;i<4;i++)); do printf "%d %s\n" $i "${Unix[$i]}"; done
len: 4
0 Debian
1 Red Hat
2 Ubuntu
3 SuSE
—– $ unset Unix[2]
—– $ echo "len: ${#Unix[@]}"; for ((i=0;i<4;i++)); do printf "%d %s\n" $i "${Unix[$i]}"; done
len: 3
0 Debian
1 Red Hat
3 SuSE
—– $ Unix=( "${Unix[@]}" )
—– $ echo "len: ${#Unix[@]}"; for ((i=0;i<4;i++)); do printf "%d %s\n" $i "${Unix[$i]}"; done
len: 3
0 Debian
1 Red Hat
2 SuSE

(note that my loop runs past the end of the array after shortening it )

38 Choperro April 15, 2014 at 5:43 pm

>>>There is no “DECLARED” maximum limit to the size of an array, …..

39 Choperro April 16, 2014 at 3:03 pm

I need to quote, don’t you?
Unix=( “${Unix[@]:0:$pos}” “${Unix[@]:$(($pos + 1)” )})

40 Choperro April 16, 2014 at 3:24 pm

Also. in 11
declare -a patter=( “${Unix[@]/Red*/}” )
It doesn’t remove array elements, it removes the first occurrence that satisfies the regular expression inside each element in the array.

41 Dan May 19, 2014 at 2:01 pm

Choperro, actually:
declare -a patter=( “${Unix[@]/Red*/}” )
Removes all occurrences that satisfies the regular expression inside each element in the array.

$ Unix=(‘Debian’ ‘Red hat’ ‘Red Hat 2′ ‘Red Hat 3′ ‘Ubuntu’ ‘Suse’ ‘Fedora’ ‘UTS’ ‘OpenLinux’);

$ patter=( ${Unix[@]/Red*/} )

$ echo ${patter[@]}
Debian Ubuntu Suse Fedora UTS OpenLinux

42 Xiaoning June 14, 2014 at 1:25 pm

Thanks for a great tutorial!

I have a txt file with a list of directories that I hope to cd into, and do the same stuff for all of them. Suppose it look like this:

“/path/to/third/dir/with space”

I try to use the code in your Example 15 for my purpose:

DIR=( `cat “$HOME/path/to/txt.txt” `)
for t in “${DIR[@]}”
echo “$t”
echo “Done!”

The above script worked fine for the first and second directory, but the third one will output this:


Instead of in one line. How can I fix that?

Also, if I add cd command in the above script:

DIR=( `cat “$HOME/path/to/txt.txt” `)
for t in “${DIR[@]}”
echo “$t”
cd “$t”
echo “Done!”

All the cd command would fail, the output looks like this:

“/path/to/first/dir” line 6: cd: “/path/to/first/dir”: No such file or directory
“/path/to/second/dir” line 6: cd: “/path/to/second/dir”: No such file or directory
“/path/to/third/dir/with line 6: cd: “/path/to/third/dir/with: No such file or directory
space” line 6: cd: space”: No such file or directory

Could you shed some light on why this happened and how should I fix it? Thank you very much!

43 Chris F.A. Johnson June 19, 2014 at 8:45 pm

>> DIR=( `cat “$HOME/path/to/txt.txt” `)

That is always the wrong way to read a file; it reads it word by word not line by line.

In bash4, the easy way is to use mapfile:

mapfile -t dir < "$filename"

44 Xiaoning June 20, 2014 at 7:06 am

Hi Chris,

Thank you for the reply!

I changed my code to use the mapfile line you suggested.

But when I run the script, this is what I got:

./ line 3: mapfile: command not found

Any ideas? Thank you!

45 ian fleming June 20, 2014 at 4:12 pm

Ran into that recently porting some scripts from RedHat to Apple OS X Mavericks. Not all bash’s support mapfile (aka readarray); it’s there in RedHat, but not in Apple’s OS X. type “man mapfile” ; if it says “No manual entry” then your system probably doesn’t have mapfile implemented. In that case, you may need to do something like the following (someone smarter than me may have a better solution):

while read line
dir[$((i++))]=$line # store $line in dir[$i] and increment $i
. . .
done < $HOME/path/to/txt.txt

46 Chris F.A. Johnson June 20, 2014 at 5:00 pm

mapfile was introduced in bash4 — more than 5 years ago.

Upgrgade your bash; it’s long overdue.

47 Xiaoning June 20, 2014 at 8:16 pm

I just check my bash version in Mac OS X Mavericks:
GNU bash, version 4.3.11(1)-release (x86_64-apple-darwin13.1.0)

Should it have mapfile?

48 Xiaoning June 20, 2014 at 8:22 pm

I also tried the read line method Ian suggested. Thanks Ian btw!

However, I still ran into the same issue that all the “echo” command gave the correct results, but I can’t cd into all the directories.

Bash returned: “./ line 14: cd: “/Users/xiaoning/some/path”: No such file or directory”

49 ian fleming June 20, 2014 at 10:57 pm

Bash 4.3.xx does have mapfile. However, OS X Mavericks’ version of bash, which should be located in /bin/bash, is 3.2.xx . I suspect you have a 2nd version of bash installed, and this is getting invoked as your startup shell. (A likely location is /opt/local/bin/bash, which is where macports installs it if it is needed by any program installed by macports. Fink may do the same.)

Your reported version of bash, 4.3, should have mapfile, but /bin/bash under OS X does not, and your script specifies to run under /bin/bash (1st line of script). To use 4.3 in your script, Find where the bash you are running (“which bash” may tell you), and change the first line of your script to invoke that bash. For example (using my example):


Regarding why your script cannot cd to “/Users/xiaoning/some/path” , I have no good explanation, assuming “/Users/xiaoning/some/path” does exist. The command

ls -ld “/Users/xiaoning/some/path”

(from the command line) will verify that the directory exists.

50 Xiaoning Wang June 21, 2014 at 11:53 am

mapfile is working now after changing the #! line to the macport bash I have installed.

Those are all valid directories that I can normally ls, or cd into. But the script for some reason is still not working…

The script I’m using now is to directly store the array of directories in a variable, and it worked just fine. However, when I try to read the same array from a file, it’s no longer working. Very strange…

51 DC August 8, 2014 at 10:11 am

how to remove lines containing any one of an array of strings from multiple files?

52 jim October 28, 2014 at 1:28 am

Using sed, write a script that takes a filename and a pattern to do the following.
If the given pattern exists in the file with the very next line starting and ending with the same pattern, delete the line that starts and ends with the given pattern. please help

Leave a Comment

Previous post:

Next post: