2

I am teaching someone how bash globbing works. I would like to show (via some bash debugging feature if possible) how bash expands the patterns prior to invoking the command. For instance I would like to do the following:

ls -l file*.txt

Then I would like bash to show what the file*.txt expanded to:

ls -l file1.txt file2.txt file3.txt file4.txt

I know how to do this using bash -x from within a script but I would prefer to do it in the interactive shell so that I don't have to introduce ideas about scripts. Is there a way to do this in interactive mode?

2 Answers2

4

You can simply echo the whole command:

echo ls -l file*.txt
Barmar
  • 9,927
0

Ok, so it's basically what I usually set in my .inputrc to get zsh like behavior in bash:

set show-all-if-unmodified on
set show-all-if-ambiguous on

Place the above lines in ~.inputrc and you're good to go. When doing something like ls *.txt just tab and it should glob everything for you and output the matching files.

EDIT: quotes from man bash:

show-all-if-ambiguous (Off) This alters the default behavior of the completion functions. If set to On, words which have more than one possible completion cause the matches to be listed immediately instead of ringing the bell.

show-all-if-unmodified (Off) This alters the default behavior of the completion functions in a fashion similar to show-all-if-ambiguous. If set to On, words which have more than one possible completion without any possible partial completion (the possible completions don't share a common prefix) cause the matches to be listed immediately instead of ringing the bell.

EDIT2: example output

$ ls *
books    dev    music   templates
$ ls *

Though it won't work if the one would try to expand the following (as it would try to complete the later argument ~/some/other/folder:

$ cd * ~/some/other/folder
ddnomad
  • 1,978
  • 2
  • 16
  • 31
  • Does it have it be in ~.inputrc? Or can I put it in ~.profile on my Ubuntu? – sshekhar1980 Jun 05 '17 at 15:10
  • It should be in ~.inputrc as it's the readline utility startup configuration file. Sincerely I have not tried to put it elsewhere (because docs clearly state where it belongs to). You may try but I doubt it would work out. – ddnomad Jun 05 '17 at 15:12
  • I tried it but it does not seem to work. Is this all I needed to put in there? sshekhar1980@:~$ cat .inputrc set show-all-if-unmodified on set show-all-if-ambiguous on – sshekhar1980 Jun 05 '17 at 15:18
  • What command have you tried to expand? It's indeed what is necessary to make it work on my machine. What is your bash version? Mine is GNU bash, version 4.4.12(1)-release. Yet it should work with an older versions as well. Have you restarted the shell after editing ~.inputrc? – ddnomad Jun 05 '17 at 15:22
  • GNU bash, version 4.3.11(1)-release (x86_64-pc-linux-gnu). Yeah after making that change I restarted the shell and rebooted once just to make sure. – sshekhar1980 Jun 05 '17 at 15:23
  • Ok, so just to clarify: the expected behavior would be slightly different from zsh so typing ls B* and pressing tab it would produce a line below with the expended results (e.g. Books Bills Bars etc). It won't substitute a B* itself. – ddnomad Jun 05 '17 at 15:25