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
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.Sign up to join this community
When you run your script:
$ ./file_lister.sh ./*.txt
The shell expands
./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:
$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
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\n' "$@"
See this question on Stack Overflow for more information on processing arguments in a script.