Artificial intelligent assistant

How to use wildcard for multiple events in shell script? I want to feed in all input matching a pattern using wildcards. Example: $ cat file_lister.sh echo $1 $ ls *.txt file1.txt file2.txt file3.txt $ ./file_lister.sh ./*.txt file1.txt But I expected, my script to print file1.txt file2.txt file3.txt

When you run your script:


$ ./file_lister.sh ./*.txt


The shell expands `./*.txt` to `./file1.txt ./file2.txt ./file3.txt`, so after the expansion you really end up executing:


$ ./file_lister.sh ./file1.txt ./file2.txt ./file3.txt


Your script prints the first argument:


echo $1


`$1` corresponds to the first argument passed to the script, which in this case is `./file1.txt` \-- I'd expect to see that instead of just `file1.txt` (unless you really ran `./file_lister.sh *.txt`).

As others have suggested in the comments, if you want to print _all_ of the arguments instead of the first, there are number of things you can do. The easiest is to change `$1` (the first argument) to `$@` (all arguments).


#!/bin/bash
printf '%s\
' "$@"


See this question on Stack Overflow for more information on processing arguments in a script.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy 67cfbea9ca27443f60b3808da2711803