# Problems passing strings within an array

**URL:** <https://community.unix.com/t/problems-passing-strings-within-an-array/325672>\
**Category:** UNIX for Dummies Questions & Answers\
**Created:** [March 13, 2013, 5:19pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672 "2013-03-13T17:19:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![hungryd](https://community.unix.com/user_avatar/community.unix.com/hungryd/32/4455_2.png) [@hungryd](https://community.unix.com/u/hungryd)\
**Post date:** [March 13, 2013, 5:19pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/1 "2013-03-13T17:19:18Z")

</div>

i have a list of apps that i need to forcequit and, from time to time, that list changes. perfect excuse to manage a single array! however, my strings with spaces aren't passing as i'd like them to. here's the simple script:

```nohighlight
#!/bin/sh

#-----Array
apps=( firefox-bin firefox JavaApplicationStub groupwise "Google\ Chrome" "Microsoft\ Word" )

for i in "${apps[@]}"
do
	killall $i
done
exit 0

```

when i generate verbose feedback, i'm seeing each app in question quit EXCEPT for those with spaces. on a mac, in the command line, i have to type the following to forcequit word and chrome:

```nohighlight
killall Microsoft\ Word
killall Google\ Chrome

```

but i can't seem to find a way to paste the correct syntax into the array to get these applications to quit as the others do. in the above script example, the result just says:

"+ for i in '"${apps[@]}"'  
+ killall 'Microsoft\' Word  
No matching processes were found"

any thoughts on what i might be missing?

---

<div class="post-metadata">

**Author:** ![verdepollo](https://community.unix.com/user_avatar/community.unix.com/verdepollo/32/1854_2.png) [@verdepollo](https://community.unix.com/u/verdepollo)\
**Post date:** [March 13, 2013, 6:03pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/2 "2013-03-13T18:03:20Z")

</div>

Use single quotes when creating the array:

```nohighlight
apps=( firefox-bin firefox JavaApplicationStub groupwise 'Google Chrome' 'Microsoft Word' )

```

Using `pkill` may also be a better choice instead of `killall` .

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 13, 2013, 6:36pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/3 "2013-03-13T18:36:03Z")

</div>

This is the actual problem:

```nohighlight
killall $i

```

```nohighlight
killall "$i" # Prevent variable with spaces from splitting 

```

Though really, I think your program could be rewritten as

```nohighlight
killall "${apps[@]}"

```

People are understandably leery of 'killall', since on some other systems it has a far more, shall we say, literal meaning.

---

<div class="post-metadata">

**Author:** ![hungryd](https://community.unix.com/user_avatar/community.unix.com/hungryd/32/4455_2.png) [@hungryd](https://community.unix.com/u/hungryd)\
**Post date:** [March 13, 2013, 7:10pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/4 "2013-03-13T19:10:35Z")

</div>

verdepollo: single quotes didn't work. tried that before i posted.  
Corona688: many thanks, for the fix. didn't think to quote the variable.

cheers, folks,  
d

---

<div class="post-metadata">

**Author:** ![MadeInGermany](https://community.unix.com/letter_avatar/madeingermany/32/6_449c533f586939b6e5f246632a46421f.png) [@MadeInGermany](https://community.unix.com/u/MadeInGermany)\
**Post date:** [March 14, 2013, 9:00am UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/5 "2013-03-14T09:00:57Z")

</div>

With pgrep|pkill one does not need an array (and not a loop)

```nohighlight
apps="firefox-bin|firefox|JavaApplicationStub|groupwise|Google Chrome|Microsoft Word"
pkill -x "$apps"

```

---

<div class="post-metadata">

**Author:** ![hungryd](https://community.unix.com/user_avatar/community.unix.com/hungryd/32/4455_2.png) [@hungryd](https://community.unix.com/u/hungryd)\
**Post date:** [March 18, 2013, 4:27pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/6 "2013-03-18T16:27:13Z")

</div>

didn't know about pkill so thank you. otherwise, i'm all about elegance. if i can do something in two lines of code, i don't care what those two lines are. yours is great but as i'm already using arrays and not bothered by them, so is the earlier example.

---

<div class="post-metadata">

**Author:** ![Corona688](https://community.unix.com/letter_avatar/corona688/32/6_449c533f586939b6e5f246632a46421f.png) [@Corona688](https://community.unix.com/u/Corona688)\
**Post date:** [March 18, 2013, 4:35pm UTC](https://community.unix.com/t/problems-passing-strings-within-an-array/325672/7 "2013-03-18T16:35:25Z")

</div>

As suggested, instead of a loop,

```nohighlight
killall "${apps[@]}"

```
