1

Is there a way to suppress globbing in parameter expansion? Consider the case of a file named literally *.xml in a directory among other xml files. Is there a way to do a parameter expansion ${param##*/} affecting only the file *.xml and not all the xml files?

param=./*.xml
echo "$param" # Displays only ./*.xml
echo $param # Displays ./*.xml ./invoice.xml ./bla.xml ...
echo ${param##*/} # Displays *.xml invoice.xml bla.xml ...
# what I want:
echo ${param##*/} # Displays only *.xml
Roland
  • 7,525
  • 13
  • 61
  • 124
  • 3
    Quote everything. e.g. `echo "${param##*/}"` – anubhava Mar 12 '20 at 10:18
  • 1
    @anubhava If you write this as an answer I will accept it – Roland Mar 12 '20 at 10:28
  • 1
    [Shellcheck](https://www.shellcheck.net/) identifies the quoting problems in the code, and what should be done about them. [Shellcheck](https://www.shellcheck.net/) finds many other problems with shell code too. It's a good idea to use it often to check your code. – pjh Mar 12 '20 at 20:32

1 Answers1

2

Converting my comment to answer so that solution is easy to find for future visitors.

Problem is not quoting your BASH expressions that allows shell to expand globs or wildcards.

You should be using quotes in shell as:

param='./*.xml'
echo "$param"
echo "${param##*/}"
anubhava
  • 761,203
  • 64
  • 569
  • 643