1

I have a folder with some xml files in it: 1.xml, 2.xml and so on. Then i am doing:

a=*.xml
echo $a

I am getting list of these xml files like this:

1.xml 2.xml

But then i am doing

a=*.qwe
echo $a

I am getting literal value of a:

*.qwe

Then I am doing

a=ls
echo $a

I am also getting literal value of a: ls

What should i do with a (how to set it properly or how to print it properly?) to always get its literal value even if i have some files with matching extension in this folder? I want always to have *.xml value regardless of files in folder.

Expected behavior: regardless of folder contents, for a=*.xml and a=*.qwe i'd like to have output - *.xml, and *.qwe. For a=ls case i dont care about output, it is just an example which i cannot understand. If i have matching files in folder, i am getting list of files, if there are no matching files - i am getting literal value of mask. But in case of ls which should return anything in any case i am getting literal value too.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
ilya
  • 119
  • 1
  • 13
  • Put the \*.xml in quotes and so a="\*.xml" – Raman Sailopal Oct 12 '20 at 15:01
  • Result is the same - list of xml files in folder. – ilya Oct 12 '20 at 15:03
  • @RamonSailopal, the quotes are needed on the echo command, not on the assignment. – Charles Duffy Oct 12 '20 at 15:04
  • my issue is not with assigned values, but with echo output. i always would like to get literal value of a in output – ilya Oct 12 '20 at 15:10
  • @markp-fuso, the glob is never expanded at assignment time (when assigning to a scalar variable and not an array); it doesn't matter how many matches exist. The right-hand side of a simple assignment does not undergo string splitting or glob expansion. – Charles Duffy Oct 12 '20 at 15:10
  • @ilya, ...and that makes the linked duplicate perfectly on-point, does it not? – Charles Duffy Oct 12 '20 at 15:11
  • By the way, consider switching to `declare -p a` instead of using `echo` at all. – Charles Duffy Oct 12 '20 at 15:13
  • @CharlesDuffy Yes, it seems linked question have explanation of my issue, but i wasnt able to find it using any of keywords i can imagine before creating my own question. – ilya Oct 12 '20 at 15:23
  • @ilya, ...well, that's the point of keeping duplicates around in the knowledgebase -- to add more keywords pointing to a given canonical explanation source. :) – Charles Duffy Oct 12 '20 at 15:30
  • BTW, see https://unix.stackexchange.com/a/65819/3113 for a more in-depth discussion of the (many) ways in which `echo`'s behavior is underspecified and varies between implementations, and thus why `printf` is considered more robust. – Charles Duffy Oct 12 '20 at 15:32

0 Answers0