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?

7
  • echo file*.txt? Commented Jun 5, 2017 at 14:55
  • Thanks Joe. That works but I would like to be able to display the intermediate step for any command such as cp file*.txt dir1/ expanded to cp file1.txt file2.txt file3.txt file4.txt dir1 Commented Jun 5, 2017 at 14:57
  • ZSH does this by default when you tab complete on a glob. Commented Jun 5, 2017 at 14:58
  • @thrig awesome! I tried that with zsh and it worked with tab completion. I will use that if I cannot figure out how to print the expanded content in bash Commented Jun 5, 2017 at 15:01
  • You can set -x in the interactive shell too... Commented Jun 5, 2017 at 15:06

2 Answers 2

4

You can simply echo the whole command:

echo ls -l file*.txt 
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 
6
  • Does it have it be in ~.inputrc? Or can I put it in ~.profile on my Ubuntu? Commented Jun 5, 2017 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. Commented Jun 5, 2017 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 Commented Jun 5, 2017 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? Commented Jun 5, 2017 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. Commented Jun 5, 2017 at 15:23

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.