The Ultimate Bash Array Tutorial With 15 Examples
The Ultimate Bash Array Tutorial With 15 Examples
com/2010/06/bash-array-tutorial/
≡ Menu
Home
Free eBook
Start Here
Contact
About
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.
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.
In bash, array is created automatically when a variable is used in the format like,
name[index]=value
echo ${Unix[1]}
1 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
$./arraymanip.sh
Red hat
If the elements has the white space character, enclose it with in a quotes.
#! /bin/bash
$cat arraymanip.sh
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.
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[@]}
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.
We can get the length of an array using the special parameter called $#.
2 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Unix[0]='Debian'
Unix[1]='Red hat'
Unix[2]='Ubuntu'
Unix[3]='Suse'
$./arraymanip.sh
4
$./arraymanip.sh
Suse Fedora
The above example returns the elements in the 3rd index and fourth index. Index always starts with zero.
./arraymanip.sh
Ubun
The above example extracts the first four characters from the 2nd indexed element of an array.
The following example, searches for Ubuntu in an array elements, and replace the same with the word ‘SCO
3 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Unix’.
$cat arraymanip.sh
#!/bin/bash
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
$./arraymanip.sh
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.
$./arraymanip.sh
AIX
In the array called Unix, the elements ‘AIX’ and ‘HP-UX’ are added in 7th and 8th index respectively.
unset is used to remove an element from an array.unset will have the same effect as assigning null to an element.
$cat arraymanip.sh
#!/bin/bash
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 arraymanip.sh
Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora' 'UTS' 'OpenLinux');
pos=3
Unix=(${Unix[@]:0:$pos} ${Unix[@]:$(($pos + 1))})
echo ${Unix[@]}
$./arraymanip.sh
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.
In the search condition you can give the patterns, and stores the remaining element to an another array as shown
below.
4 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
$ cat arraymanip.sh
#!/bin/bash
declare -a Unix=('Debian' 'Red hat' 'Ubuntu' 'Suse' 'Fedora');
declare -a patter=( ${Unix[@]/Red*/} )
echo ${patter[@]}
$ ./arraymanip.sh
Debian Ubuntu Suse Fedora
The above example removes the elements which has the patter Red*.
$ ./arraymanip.sh
Debian Red hat Ubuntu Fedora UTS OpenLinux
UnixShell=("${Unix[@]}" "${Shell[@]}")
echo ${UnixShell[@]}
echo ${#UnixShell[@]}
$ ./arraymanip.sh
Debian Red hat Ubuntu Suse Fedora UTS OpenLinux bash csh jsh rsh ksh rc tcsh
14
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.
UnixShell=("${Unix[@]}" "${Shell[@]}")
unset UnixShell
echo ${#UnixShell[@]}
$ ./arraymanip.sh
0
5 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
$ cat loadcontent.sh
#!/bin/bash
filecontent=( `cat "logfile" `)
for t in "${filecontent[@]}"
do
echo $t
done
echo "Read file content!"
$ ./loadcontent.sh
Welcome
to
thegeekstuff
Linux
Unix
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.
6 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Tagged as: Bash Array String, Bash Arrays, Bash Script Array, Bash Scripting Tutorial, Bash Tutorial, Echo
Array, Linux Array, Unix Array
Good article.
Thanks,
Tanmay
Link
Gabriele June 3, 2010, 6:47 am
Great stuff!!!
Regards
Gabriele
Link
iambryan June 3, 2010, 8:52 am
Great examples
7 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
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.
Link
Ramesh Natarajan June 3, 2010, 10:11 pm
@Tanmay, @Bryan,
Thanks for pointing out the issues. They are fixed now.
Link
hari June 3, 2010, 11:50 pm
best best
best
Link
Chris F.A. Johnson June 4, 2010, 8:01 am
1. “echo ${Unix[1]}” will not necessarily print element 1 from the array. For example:
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.)
4. More accurately, ${#arrayname[@]} gives you the number of elements in the array.
(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.)
8 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Since bash4, this can be done even more efficiently with the mapfile builtin:
for t in "${filecontent[@]}"
Link
anonymous June 4, 2010, 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:
{
IFS=$’\n’
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.
Link
TheFu June 7, 2010, 9:36 am
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.
Link
sbaginov June 9, 2010, 4:47 am
Link
WaS June 21, 2010, 3:36 am
and logfile have one “*” you get a list of archives in your directory, how i can solve it?
Thx
Link
Chris F.A. Johnson June 21, 2010, 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
9 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
expanded:
Or:
echo “$logfile”
(Always quote variable references unless you have a good reason not to.)
Link
Toni Kukul October 8, 2010, 5:47 pm
To read a file into an array it’s possible to use the readarray or mapfile bash built-ins.
readarray < filename
or
mapfile < filename
Link
Anderson Venturini February 28, 2012, 6:08 am
Link
ak March 11, 2012, 10:50 am
Good Examples. Thank you for hard work and clear explanations. Error in number 12: Suse is omitted
from the copied array.
Link
Vivek May 30, 2012, 8:07 pm
Hi,
How can I have my shell script generate cntrC without me typing cnrlC?
Vivek.
Link
Chris F.A. Johnson May 31, 2012, 12:21 pm
Link
Vivek May 31, 2012, 8:54 pm
10 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
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.
Example:
for a in $(seq 1 10)
do
done
Vivek.
Link
Chris F.A. Johnson June 1, 2012, 12:23 pm
for n in {1..10}
do
: whatever
done &
Link
Dennis Dashkevich July 12, 2012, 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]}.
Link
h October 29, 2012, 1:25 am
white space in elements not getting eliminated even though quotes are used
Link
Josh January 2, 2013, 5:41 pm
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:
11 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Link
Chris F.A. Johnson January 2, 2013, 7:23 pm
Link
Chris F.A. Johnson January 8, 2013, 6:41 pm
Link
Angelo January 23, 2013, 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:
http://en.wikipedia.org/wiki/SuSE)
Link
Angelo January 23, 2013, 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.
#!/bin/bash
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 ;;
esac
}
12 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
$ sh test-contains.sh
String test 1: OK
String test 2: FALSE, but should be TRUE
Numeric test: ./test-contains.sh: line 4: ${1[@]}: bad substitution
1
Quoted-numeric test: ./test-contains.sh: line 4: ${1[@]}: bad substitution
1
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.
Link
Heriel Uronu@udom April 30, 2013, 7:18 am
Link
x31eq July 17, 2013, 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
13 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Link
Hans November 29, 2013, 1:49 pm
Thank you.
Link
mug896 January 2, 2014, 6:31 am
Link
Chris F.A. Johnson January 2, 2014, 11:57 am
mug896,
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.
Link
Chris F.A. Johnson January 2, 2014, 2:10 pm
My mistake, mug896; your code will read the file into a single element of the array. You can see that by:
It would have read each word into a separate element of the array.
Link
Offirmo January 9, 2014, 8:21 am
Link
Erick January 24, 2014, 5:43 pm
Thanks a lot!
14 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Link
Robert Vila February 23, 2014, 10:57 pm
Error messages:
>>>> “declare: not found”
or
>>>> “Unix[0]=Debian: not found”
Link
Chris F.A. Johnson February 25, 2014, 3:14 pm
Robert, make sure you are using bash to interpret the script.
Link
Robert Mark Bram February 27, 2014, 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.
Fri Feb 28 – 12:53 PM > Unix=(‘Debian’ ‘Red hat’ ‘Ubuntu’ ‘Suse’ ‘Fedora’ ‘UTS’ ‘OpenLinux’);
Fri Feb 28 – 12:53 PM > echo ${#Unix[@]}
7
Fri Feb 28 – 12:53 PM > pos=3
Fri Feb 28 – 12:53 PM > echo ${Unix[$pos]}
Suse
Fri Feb 28 – 12:53 PM > Unix=(“${Unix[@]:0:$pos}” “${Unix[@]:$(($pos + 1))}”)
Fri Feb 28 – 12:53 PM > echo ${Unix[$pos]}
Fedora
Fri Feb 28 – 12:53 PM > echo ${Unix[@]}
Debian Red hat Ubuntu Fedora UTS OpenLinux
Fri Feb 28 – 12:53 PM > echo ${#Unix[@]}
6
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
Link
ian fleming March 13, 2014, 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[@]” )
Example:
15 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
(note that my loop runs past the end of the array after shortening it )
Link
Choperro April 15, 2014, 5:43 pm
Link
Choperro April 16, 2014, 3:03 pm
Link
Choperro April 16, 2014, 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.
Link
Dan May 19, 2014, 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*/} )
16 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
$ echo ${patter[@]}
Debian Ubuntu Suse Fedora UTS OpenLinux
Link
Xiaoning June 14, 2014, 1:25 pm
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/first/dir”
“/path/to/second/dir”
“/path/to/third/dir/with space”
…
#!/bin/bash
DIR=( `cat “$HOME/path/to/txt.txt” `)
for t in “${DIR[@]}”
do
echo “$t”
done
echo “Done!”
The above script worked fine for the first and second directory, but the third one will output this:
“/path/to/third/dir/with
space”
#!/bin/bash
DIR=( `cat “$HOME/path/to/txt.txt” `)
for t in “${DIR[@]}”
do
echo “$t”
cd “$t”
done
echo “Done!”
All the cd command would fail, the output looks like this:
“/path/to/first/dir”
test.sh: line 6: cd: “/path/to/first/dir”: No such file or directory
“/path/to/second/dir”
test.sh: line 6: cd: “/path/to/second/dir”: No such file or directory
“/path/to/third/dir/with
test.sh: line 6: cd: “/path/to/third/dir/with: No such file or directory
space”
test.sh: line 6: cd: space”: No such file or directory
17 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Could you shed some light on why this happened and how should I fix it? Thank you very much!
Link
Chris F.A. Johnson June 19, 2014, 8:45 pm
That is always the wrong way to read a file; it reads it word by word not line by line.
Link
Xiaoning June 20, 2014, 7:06 am
Hi Chris,
Link
ian fleming June 20, 2014, 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):
i=0
while read line
do
dir[$((i++))]=$line # store $line in dir[$i] and increment $i
...
done < $HOME/path/to/txt.txt
Link
Chris F.A. Johnson June 20, 2014, 5:00 pm
Link
Xiaoning June 20, 2014, 8:16 pm
18 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Link
Xiaoning June 20, 2014, 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: “./test.sh: line 14: cd: “/Users/xiaoning/some/path”: No such file or directory”
Link
ian fleming June 20, 2014, 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):
#!/opt/local/bin/bash
ls -ld “/Users/xiaoning/some/path”
(from the command line) will verify that the directory exists.
Link
Xiaoning Wang June 21, 2014, 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…
Link
DC August 8, 2014, 10:11 am
how to remove lines containing any one of an array of strings from multiple files?
Link
jim October 28, 2014, 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,
19 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
delete the line that starts and ends with the given pattern. please help
Link
Yves B April 24, 2015, 7:02 am
Thanks for tip no15. “Load Content of a File into an Array”. Exactly what I was looking for.
Link
John Allsup June 1, 2015, 4:56 am
Care needs to be taken with quotes, both in general, and especially when playing with arrays. The
following is a simple bash script that collects together working examples of the things you demonstrate
above. Note that the file hx used at the end just contains a few lines of text, some of which contain spaces.
#!/bin/bash
declare -a A
A[3]=flibble
echo “$A[3]” might be flibble, the third item, but isnt
echo “${A[3]}” should be flibble, the third item, note the braces
echo “${A[@]}” is contents of array
echo “${#A[@]}” is length of array
echo “${#A[3]}” should be 7, length of flibble
echo “${A[3]:2:3}” should be ibb, the three characters starting at pos 2
echo “${A[@]/ibb/bone}” is search and replace for each item
A=(“${A[@]}” “wibble”)
echo A is now “${A[@]}”
echo now
declare -a B=(“${A[@]}”)
echo Third item is “${B[3]}”
echo Zeroth item is “${B[0]}”
echo So copying arrays this way does not preserve string keys — it reindexes
declare -a C
C[wibble]=wobble
echo “${C[wibble]}” shows keys are strings, not contiguous integers
declare -a D
D=(“a b c d e” “c d f t g”)
echo D is “${D[@]}”
echo Length of D is “${#D[@]}”
echo Length of “D[0]” is “${#D[0]}”
echo “D[0] is ‘${D[0]}'”
declare -a E=( ${D[@]} )
echo E is “${E[@]}”
echo Length of E is “${#E[@]}”
echo Length of “E[0]” is “${#E[0]}”
echo “E[0] is ‘${E[0]}'”
declare -a F=( ${D[@]/a*/} )
echo F is “${F[@]}”
echo Length of F is “${#F[@]}”
echo Length of “F[0]” is “${#F[0]}”
echo “F[0] is ‘${F[0]}'”
declare -a G=( “${D[@]/a*/}” )
echo G is “${G[@]}”
20 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Link
Griff November 6, 2015, 11:04 am
+1 on x31eq’s comment about the quoting. It also means the value of ${#Unix[@]} is wrong. It would be
great if you could correct this.
Link
Sam April 28, 2016, 5:37 pm
Example: unzip $A | mysql -u root -p $B ## Here the problem is it executes the ‘A’ portion for each of the
‘B’ elements
Link
srinivas May 25, 2016, 11:49 pm
Hi,
I have single item ‘red hat’ in array like array[‘red hat’]. I want split the array from single index to 2
indexes like array[‘red’ ‘hat’].please suggest me with a solution
Link
Sneha June 20, 2016, 11:54 pm
21 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Hi,
but if I do:
echo ${#arrayname[@]}
it gives: 4 instead of 3
and
for arr in “${arrayname[@]}”; do; echo “$arr”; done
gives:
abc
def
‘ghi
jkl’
instead of:
abc
def
ghi jkl
Even:
arrayname=( $DBVAL )
does not work.
Although, if I declare the array with the hardcoded values (not get it from function/from any variable),
then it works fine.
declare -a arrayname=(‘abc’ ‘def’ ‘ghi jkl’)
echo ${#arrayname[@]}
gives: 3
Please help.
Link
Gulab June 27, 2016, 7:25 am
how to import multiple directory in array in runtime and check if directory is present or not ?
Link
Leave a Comment
Name
22 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Website
Comment
Next post: Lzma Vs Bzip2 – Better Compression than bzip2 on UNIX / Linux
Previous post: VMware Virtualization Fundamentals – VMware Server and VMware ESXi
Search
EBOOKS
Linux 101 Hacks 2nd Edition eBook - Practical Examples to Build a Strong Foundation in Linux
Bash 101 Hacks eBook - Take Control of Your Bash Command Line and Shell Scripting
Sed and Awk 101 Hacks eBook - Enhance Your UNIX / Linux Life with Sed and Awk
Vim 101 Hacks eBook - Practical Examples for Becoming Fast and Productive in Vim Editor
Nagios Core 3 eBook - Monitor Everything, Be Proactive, and Sleep Well
23 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
POPULAR POSTS
12 Amazing and Essential Linux Books To Enrich Your Brain and Library
50 UNIX / Linux Sysadmin Tutorials
50 Most Frequently Used UNIX / Linux Commands (With Examples)
How To Be Productive and Get Things Done Using GTD
30 Things To Do When you are Bored and have a Computer
Linux Directory Structure (File System Structure) Explained with Examples
Linux Crontab: 15 Awesome Cron Job Examples
Get a Grip on the Grep! – 15 Practical Grep Command Examples
Unix LS Command: 15 Practical Examples
15 Examples To Master Linux Command Line History
Top 10 Open Source Bug Tracking System
Vi and Vim Macro Tutorial: How To Record and Play
Mommy, I found it! -- 15 Practical Linux Find Command Examples
15 Awesome Gmail Tips and Tricks
15 Awesome Google Search Tips and Tricks
RAID 0, RAID 1, RAID 5, RAID 10 Explained with Diagrams
Can You Top This? 15 Practical Linux Top Command Examples
Top 5 Best System Monitoring Tools
Top 5 Best Linux OS Distributions
How To Monitor Remote Linux Host using Nagios 3.0
Awk Introduction Tutorial – 7 Awk Print Examples
How to Backup Linux? 15 rsync Command Examples
The Ultimate Wget Download Guide With 15 Awesome Examples
Top 5 Best Linux Text Editors
Packet Analyzer: 15 TCPDUMP Command Examples
The Ultimate Bash Array Tutorial with 15 Examples
3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
Unix Sed Tutorial: Advanced Sed Substitution Examples
UNIX / Linux: 10 Netstat Command Examples
The Ultimate Guide for Creating Strong Passwords
6 Steps to Secure Your Home Wireless Network
Turbocharge PuTTY with 12 Powerful Add-Ons
CATEGORIES
Linux Tutorials
24 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
Vim Editor
Sed Scripting
Awk Scripting
Bash Shell Scripting
Nagios Monitoring
OpenSSH
IPTables Firewall
Apache Web Server
MySQL Database
Perl Programming
Google Tutorials
Ubuntu Tutorials
PostgreSQL DB
Hello World Examples
C Programming
C++ Programming
DELL Server Tutorials
Oracle Database
VMware Tutorials
My name is Ramesh Natarajan. I will be posting instruction guides, how-to, troubleshooting tips
and tricks on Linux, database, hardware, security and web. My focus is to write articles that will either teach you
or help you resolve a problem. Read more about Ramesh Natarajan and the blog.
Contact Us
Email Me : Use this Contact Form to get in touch me with your comments, questions or suggestions about this
site. You can also simply drop me a line to say hello!.
Follow us on Google+
Follow us on Twitter
Support Us
25 of 26 9/30/2016 7:27 AM
The Ultimate Bash Array Tutorial with 15 Examples http://www.thegeekstuff.com/2010/06/bash-array-tutorial/
26 of 26 9/30/2016 7:27 AM