To Supress *

Hi Team ,

I want supress the meaning of * while passing it as parameter.
I have file which contains file format and destination directory.

let say abc* |/home/xyz

I had function which will read these values and pass it to another function.
Code looks like below

func1 ()
{
file_format = (first field value from the file
remote_dir= (second field from the file)
func2  file_format remote_dir
}

function 2   
{
echo  $1
echo  $2
)

when I printed I am getting

$1 as abc1.dat and $2 has abc2.dat

.
but I want it to be

$1 as abc* and $2 has /home/xyz

I am using korn shell OS AIX

Put your variables in double-quotes to suppress wildcard expansion and splitting.

echo "$1"
1 Like

It is not working. I tried that option also before posting my query

In what way is it not working? Show exactly what you do and exactly what happens. Word for word, keystroke for keystroke. Don't paraphrase anything when you don't know what's relevant or not.

Also

func2  "$file_format" "$remote_dir"
abc\*  or 'abc*'
1 Like

Understand that once expansion of the wildcard has happened it can't be taken back. The shell itself expands the wildcard before it passes what it has expanded the wildcard to to the respective program.

Here is an example: suppose you call the program "ls". You enter on the commandline

ls *

first, the shell notices the wildcard and replaces it with all the filenames in the directory. Let us suppose there are three files, "file.a", "file.b" and "file.c". The shell replaces the asterisk for these names, arriving at:

ls file.a file.b file.c

This is what "ls" will be presented, because all the expanding is done before "ls" is even called.

So far, so good. Now suppose you do:

subfunc ()
{
   echo "$1"
}

subfunc *

You have protected the argument inside the function, but because you didn't protect the wildcard when calling the function "subfunc()" will only be presented the result of its expansion. You will need:

subfunc ()
{
   echo "$1"
}

subfunc "*"

Also notice, that shell interpretation "eats away" protection:

var="echo *"
$var

The asterisk inside the variable declaration seems to be protected, but by expanding the line "$var" the shell removes the quotation and when the line is executed it is interpreted a second time. Both the following modifications would work, both times it boils down to doubly double-quote, so to say:

var="echo *"
"$var"

var="\"echo *\""
$var

I hope this helps.

bakunin

1 Like