William William - 12 days ago 6
Bash Question

Shellscript Newbie - Issue with Wildcards & Arguments in a Script

As the title suggests I'm just starting out with shellscript so don't really know much about it!

My question is this - why does the following wildcard work in bash

mv *.txt /root/Dustbin


But my shellscript file only moves one .txt file and not all? I install the script and call it as follows

del *.txt


This is the script:

#!/bin/sh
#sh scriptname del

mv -u $1 /root/Dustbin

#END OF SCRIPT


Hope my question makes sense

Thanks in advance for any help

Answer

$1 is the first argument of the script. So even if there are multiple files matching *.txt, only the first one will be moved. If you want to move all of them, use "$@" instead:

#!/bin/sh

mv -u "$@" /root/Dustbin
Comments