Created
December 9, 2011 06:45
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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