I am trying to store some function parameters in a bash array, but some of them are defined from the content of environment variables and they do not actually "expand" to their actual values (I hope to be clear enough):
Example:
fileprefix='20200222_*'
test=1
args=( '-F' )
[[ "${test}" == 1 ]] && args+=( '-la "${fileprefix}"' )
But when executing it it raises an error:
$ ls "${args[@]}"
ls: invalid option -- ' '
Try 'ls --help' for more information.
and:
$ echo "${args[@]}"
-F -la "${fileprefix}"
but:
$ echo "${fileprefix}"
20200222_*
I would expect:
$ ls "${args[@]}"
-rw-r--r-- 1 user user 0 Jul 4 19:37 20200222_a.txt
-rw-r--r-- 1 user user 0 Jul 4 19:38 20200222_b.txt
-rw-r--r-- 1 user user 0 Jul 4 19:39 20200222_c.txt
-rw-r--r-- 1 user user 0 Jul 4 19:40 20200222_d.txt
Hence my question; how would you "expand" the content of the ${fileprefix}
variable when executing the ls
command with the array of parameters?
I didn't found something interesting here: https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_10_02.html
declare -p args
is more useful for inspecting an arraySyntax error: "(" unexpected (expecting "fi")
error:#!/bin/sh
Great, haha! :D (the question may still be useful for bash scripts anyway)