I don't understand why shell expansion isn't happening on the . Why am I getting /shell_expansion/.txt instead of /shell_expansion/test1.txt and so on?
An alternate approach could be with: ./test_script.sh
#!/bin/bash
list_dir="."
# list_dir is set to current pwd,
# as you have shown your *txt files reside in the same pwd as your script...
# this actualy means, the 'cd $list_dir;' part would not be required,
# but this would let you get the plain file names, rather than with the full path.
for filename in $(cd "$list_dir";ls *.txt | head -n4 )
do echo "$filename"
done
Not that we don't believe you, but what you're saying doesn't make sense...
Please show us the output from the commands:
pwd
ls -l /shell_expansion/*.txt $HOME/shell_expansion/*.txt ./shell_expansion/*.txt ./*.txt
(both on standard output and on standard error output).
And, just for the fun of it, please explain why you want to print the names of all of the names of files ending in the string .txt in a directory four times.