Assuming I want to run commnand stored inside the variable with nullglob turned on. For example:
shopt -s nullglob
a="echo [foo]bar"
${a}
This gives me an empty output due to the nullglob option of course, but I want the following output (which I am not able to get):
[foo]bar
I tried escaping [] with \ but that just gives me:
\[foo\]bar
What is the correct way of escaping it?
EDIT (clarification with some context):
I have a script like this:
shopt -s nullglob
for file in tmp/*.pdb; do
base="$(basename ${file} .pdb)"
a="command --option [${base}]foo"
${a}
done
shopt -u nullglob
What I wanted to achieve is to run command for each file with an option which has [] as ordinary characters (without any matching). The nullglob here was used just for the for loop.
That is if "tmp" contains "a.pdb" run:
command --option '[a]foo'
and nothing if no such file is present.
In a meantime I figured out that moving "shopt -u nullglob" as the first command in the for loop seems to solve the problem. However, I am curious if I can somehow escape the [] even with nullglob.
[foo]bar
and ii) if there isn't?nullglob
. Why not just unsetnullglob
? What am I missing?