Skip to content

Instantly share code, notes, and snippets.

@coderjoe
Created December 9, 2011 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coderjoe/1450520 to your computer and use it in GitHub Desktop.
Save coderjoe/1450520 to your computer and use it in GitHub Desktop.
Storing the output of a command in an array and passing it to another program as arguments
#!/bin/bash
#Create an empty directory
#Save this script as test.sh within it
#create three example files
touch one\ file.txt
touch two.txt
touch three\ files\ here.txt
#Our directory contains 4 files, this script and the example files
IFS=$'\n' #change the separator to newline only
stuff=( $(ls) ) #put the output of the application into an array
echo "From an array..."
wc -l "${stuff[@]}" #use the array as arguments to wc -l
echo "By hand..." #this exists only to show that it's the same output
wc -l one\ file.txt two.txt three\ files\ here.txt test.sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment