Sample Header Ad - 728x90

Using a literal asterisk (*) in a filename within command substitution in bash

2 votes
1 answer
486 views
how do you supply a literal asterisk * when specifying a filename to a command substitution in bash? Consider the following directory:
host:/tmp/backtick-test# ls -l
total 0
-rw-r--r--  1 root  wheel  0 29 Jun 23:18 test
-rw-r--r--  1 root  wheel  0 29 Jun 23:18 test*
I can't work out how to specify the second file test* with $(). List both files:
host:/tmp/backtick-test# ls test*
test  test*
List only the file with the trailing *:
host:/tmp/backtick-test# ls test\*
test*
Now with commmand expansion:
host:/tmp/backtick-test# echo $(ls test*)
test test test*
Returns three filenames.
/tmp/backtick-test# echo $(ls -al "test*")
-rw-r--r-- 1 root wheel 0 29 Jun 23:18 test test*
Better, returns two but I only want the second one.
Asked by Scott (103 rep)
Jun 29, 2025, 01:42 PM
Last activity: Jun 30, 2025, 06:13 AM