Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a string with a leading Asterick *

Status
Not open for further replies.

cptk

Technical User
Mar 18, 2003
305
US
How can I return a string with a leading asterick (i'm using ksh)? With all the other metacharacters, I can return their "symbol", but not the asterick!

ex:
>x="*.c";echo $x
returns --> all files matching *.c , which is not what I want!

I want to return just the string *.c !!!

If I put a character in front of the *, e.g. x="w*.c", it will return the string w*.c

Other attempts I've tried that failed ...
>x=\*.c;echo $x --> returns file list
>x=*.c;echo $x --> returns file list
>x="\*.c";echo $x --> \*.c
>x='*.c';echo $x --> returns file list

Is this doable?



 
x='*.c';echo $x

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Thanks ...
I've tried single quotes, doesn't work ... returns a file list as well..
 
#!/bin/ksh

a='*.c'

echo &quot;$a&quot;


vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
quoting the echo return variable works !!
echo &quot;$x&quot;
Thanks ...

I'm assumming this works because when the echo cmd is executed without the double quotes, it actually performing the cmd *.c . Thus one would believe that just typing in *.c at a prompt would yield a list of files ... but it doesn't, so I'm still a little confussed.
 
The reason is because the shell replaces variables and expands wildcards BEFORE the command is executed. Unless it's protected by single or double quotes (each works differently).

For example, your command...
[tt]
echo $x
[/tt]
...is first translated to...
[tt]
echo *.c
[/tt]
...then the wildcard is expanded making it...
[tt]
echo a.c b.c c.c j.c zzz.c
[/tt]
...which is then executed producing the output...
[tt]
a.c b.c c.c j.c zzz.c
[/tt]
When you enclose it in double quotes, the variable is replaced, but the wildcard is NOT expanded. So, from the command...
[tt]
echo &quot;$x&quot;
[/tt]
...the shell replaces the variable to make it...
[tt]
echo &quot;*.c&quot;
[/tt]
...which then executes and gives the output...
[tt]
*.c
[/tt]
If you used single quotes, this keeps the shell from touching ANYTHING in the single quotes. So, if your command looked like...
[tt]
echo '$x'
[/tt]
...it would just execute and give the output...
[tt]
$x
[/tt]
Hope this helps.

 
It's called 'globbing' (No I don't know why its glob), and you can turn it on and off

#!/bin/ksh

echo *.c
set -f
echo *.c
set +f
echo *.c
 
Salem --
That was the &quot;trick&quot; I was looking for! Although I got around it by quoting the variable, it makes reading the code a little more difficult visually when adding additional quotes. Adding your idea makes for easier reading.

With your idea, I'm just applying a set -f before
I &quot;monkey&quot; with the string, then I apply a set +f after. I knew there was a &quot;simple&quot; way because I've done it long ago. Probably at that time I had the file name generation turned off (i.e. - set -f) without even realizing it.

Thanks, I knew I wasn't going crazy!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top