Bash script question: command substitution and arrays

Hey All,

I'm writing up some scripts and I can see advantages to doing what I need to by hacking up a Bash script. In pseudo code this is what I want to do:

#!/bin/bash
# assume current directory has X number of .txt files
output=`ls *.txt`
read each filename into an array

So for example if the current directory contains "linode.txt cool.txt whatever.txt" I would like to have each of those filenames in an array, for example:

array[0] contains linode.txt

array[1] contains cool.txt

array[2] contains whatever.txt

Is there a simple way to do this? Note that I would prefer not to use awk in this situation as it is going to be used later after I parse out some data. I suppose I could also do this in Perl, but I would like to focus on learning some bash scripting right now.

1 Reply

Ugh, 3 seconds more of searching and I found my answer. (Wish I could delete my posts)

For completeness, I needed to use parens around my command :)

output=(ls *.txt)

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct