Consider the script print_folder.sh
:
echo $1
ls -F $1
This simple script expects a user to supply the name of a folder through the first argument. The script will first print out the name of the folder and then it's contents.
Now, let's consider two cases of $1
: the one when $1
doesn't and the one when $1
does contain spaces.
Case 1: No Spaces
[nikola@leviathan ~]$ ./print_folder.sh Pictures/
Pictures/
Gifs/ Miscellaneous/ Screenshots/ Wallpapers/
No problems, everything works fine.
Case 2: Spaces
[nikola@leviathan ~]$ ./print_folder.sh Templates/URxvt\ Colorschemes/
Templates/URxvt Colorschemes/
ls: cannot access 'Templates/URxvt': No such file or directory
ls: cannot access 'Colorschemes/': No such file or directory
See, here is the problem. The cause of it is obviously that $1
has the value of Templates/URxvt Colorschemes
instead of Templates/URxvt\ Colorschemes
.
How can I make
$1
contains the backslashes that signify special characters instead of removing them? Also, keep in mind that I will not accept workarounds like making ls
read spaces "normally" (i.e.
is read as a space in a folder's name instead of another folder's name) since I don't use $1
as an argument only to ls
so that may be not possible with other commands.