Spaces in File Name issue

Hi,

I have a small issue while using variables for a file name in UNIX.

I have file name like "abc def 123.txt"
Folder will be in /zzz/xxx/yyy

I created 2 variables Folder = '/zzz/xxx/yyy'
file = 'abc def 123.txt'

While using this in grep command i am getting an error

i used like '$Folder"/"$file'

could u quote me where i am going wrong.I think small tweak will serve the purpose.

But i am missing it somewhere

Thanks in Advance,
San :slight_smile:

Always helps to see the actual script and the actual error message.

Two points:

No spaces before/after the equals sign

Folder='/zzz/xxx/yyy'
file='abc def 123.txt'

Which quotes?
Anything between single quotes is protected from the shell and no substitution will take place. You need double quotes to preserve the spaces but definitely not single quotes when there is a variable involved.

grep "mystring" "$Folder"/"$file"

This might help you.

Single versus double quotes in BASH | WiredRevolution.com

Thanks for the Help
"$Folder"/"$file" this serve the purpose

I am giving in other way

Thanks again for quick response

Cheers,
San

I can't take credit for the meat and potatoes of this script, but I can't remember who gave it to me and from what forum. But it does replace : in file names and replaces them with a - and then changes the file extention to dat. This might work for you. I hope this helps. I use this to take files from UNIX and make them readable in DOS, and then clean up after itself. :slight_smile:

#!/bin/csh
cd /home/files
# cp $1 /tmp
cp *DH /tmp
cd /tmp
ls -1 *.DH > /tmp/DH.list
set filelist=`cat /tmp/DH.list`
foreach x ($filelist)
        set newname=`echo $x|sed 's/:/-/g'`
        echo $newname
        cp $x /tmp/$newname
end
cd /tmp
ls -1 *.DH > /tmp/DH1.list
set filelist=`cat /tmp/DH1.list`
foreach x ($filelist)
        set newname=`echo $x|sed 's/DH/dat/g'`
        echo $newname
        cp $x /tmp/$newname
end
rm *:*:*.dat
zip dh *.dat
allocate floppy_0 
rm -r /floppy/floppy0/*
cp dh.zip /floppy/floppy0
deallocate floppy_0
rm *.DH
rm *.dat
rm dh.zip
rm DH*

##
Subsitute your file names and spaces.

best way to retrieve a variable is to use the below syntax:-

"${variable_name}"

BR

the "${Varname}" is available in bash only.

---------- Post updated at 07:19 AM ---------- Previous update was at 07:18 AM ----------

sorry not only in bash but also in KSH.
BR

---------- Post updated at 07:22 AM ---------- Previous update was at 07:19 AM ----------

kindly find some details and example below:-

Quoting means just that, bracketing a string in quotes. This has the effect of protecting special characters in
the string from reinterpretation or expansion by the shell or shell script. (A character is "special" if it has an
interpretation other than its literal meaning, such as the wild card character -- *.)
bash$ ls -l [Vv]*
-rw-rw-r-- 1 bozo bozo 324 Apr 2 15:05 VIEWDATA.BAT
-rw-rw-r-- 1 bozo bozo 507 May 4 14:25 vartrace.sh
-rw-rw-r-- 1 bozo bozo 539 Apr 14 17:11 viewdata.sh
bash$ ls -l '[Vv]*'
ls: [Vv]*: No such file or directory