Articles

escaping * in korn shell

July 11, 2005 at 20:11:50
Specs: sun OS, 8/16G

When trying to escape special character * - it doesn't seem to work. In korn shell trying to store a local variable as follows
sample=test*
echo $sample - gets all the file names starting with test* , instead i want to literally store the value test* into a variable. I tried escaping with \, with single quotes, with double quotes - nothing helped. Please provide suggestions
Greatly appreciate it.
Thanks

See More: escaping * in korn shell

Report •


#1
July 12, 2005 at 06:24:20

I don't have this issue on HP-UX.

sample=test\*
echo $sample
test*

sample2=$sample
echo $sample2
test*


Report •

#2
July 12, 2005 at 14:19:30

You might not have a file name starting with
'test' in that directory where you are.

Did you try creating a sample file named 'test' from the directory where you trying to set this variable 'sample'

Please let me know results, when you get a chance.
Thank you for your reply


Report •

#3
July 13, 2005 at 00:10:48

I have several files beginning with test in this particular directory. The following works for me (Solaris):

export SS=test*

echo $SS = test.ksh test2.ksh testall.ksh .. and so on

echo "$SS" = test*

I hope this helps.


Report •

Related Solutions

#4
July 13, 2005 at 06:48:56

Sorry 'bout that.  I have now created several filenames beginning with test.

The issue is not in assigning the string to the variable, but actually with the echo.  The echo command evaluates each expression to be echoed, and that's when the asterisk gets expanded.

In the example that follows, I assign a 5-character string to sample, and a display of the length of sample shows that it stored 5 characters.  Then see the results when I echo $sample with and without double-quotes.

sample=test*
echo ${#sample}
5
echo "$sample"
test*
echo $sample
test1 test2 test3 test4

Also, you can disable filename generation with the shell -f option:

echo $-
ism
set -f
echo $-
isfm
echo $sample
test*

set +f
echo $-
ism
echo $sample
test1 test2 test3 test4


Report •

#5
July 13, 2005 at 14:35:46

Thank you very much for the detailed explanation Jim Boothe

I greatly appreciate the answers
Jim Boothe and cislord.


Report •


Ask Question