Sample script to copy /tmp/template.txt
file to any directory as specified in $1
.
copy_script.sh
if [ $# -eq 0 ]; then
echo No Argument
echo "Usage: $0 <path>"
else
cp /tmp/template.txt $1
fi
Before
wolf@linux:~$ ls -lh
total 4.0K
drwxrwxr-x 2 wolf wolf 4.0K Dis 31 10:08 'another directory'
wolf@linux:~$
Testing script
wolf@linux:~$ copy_script.sh
No Argument
Usage: /home/wolf/bin/copy_script.sh <path>
wolf@linux:~$
Testing code with current path
wolf@linux:~$ copy_script.sh .
After (it works)
wolf@linux:~$ ls -lh
total 8.0K
drwxrwxr-x 2 wolf wolf 4.0K Dis 31 10:08 'another directory'
-rw-rw-r-- 1 wolf wolf 12 Dis 31 10:26 template.txt
wolf@linux:~$
Then, I test with another directory which has space in it.
This time, it doesn't work anymore even though the directory has been put it quote (both single/double quote doesn't work).
wolf@linux:~$ copy_script.sh 'another directory'
cp: target 'directory' is not a directory
wolf@linux:~$
wolf@linux:~$ ls -lh another\ directory/
total 0
wolf@linux:~$
How do I make this work with directory name with space in it?
"$1"
. See for example When is double-quoting necessary? – steeldriver Dec 31 '20 at 02:40