Hiding Things from the Shell

Team-Fly    

Solaris™ Operating Environment Boot Camp
By David Rhodes, Dominic Butler
Table of Contents
Chapter 5.  Shells


We have just looked at an example where we needed to hide a wildcard from the shell so that the find command could see it and deal with it instead. In that example we used quotes to hide the wildcard from the shell, but as we shall see there are a number of other ways of hiding things from the shell and a number of other reason we may wish to do so.

When we looked at shell variables earlier in the chapter we saw that if the shell sees any word beginning with a dollar sign it will assume it is a variable and will replace it with the contents of that variable. If there is no such variable defined then the shell will simply remove it. So if we had written a shell script that was a simple betting game and we wanted to print a message telling the user he had just won $1,000 you might try the following:

 echo You have just won $1000 

The shell will see the "$" and think "Ah! We have an environment variable here; let's replace it with its value."

The shell will actually recognize the $1 of the $1,000 and will think we want to print the contents of $1 followed by "000." If $1 has no value (because the shell was invoked with no parameters) then it will remove the $1 and print:

 You have just won 000 

If $1 contained the word "fred" then we would get:

 You have just won fred000 

Neither of these is what we wanted but if we could hide the "$" from the shell then we would be OK.

We saw that we could hide a wildcard using single (') or double (") quotes, but variables are different. If we use double quotes the shell will still see the "$," but if we use single quotes the shell won't. In this respect single quotes can be thought of as being stronger than double quotes.

So, to get the line we want we could type:

 echo You have just won '$1000' 

But it probably looks neater to type:

 echo 'You have just won $1000' 

Both of them will cause the correct result to be printed to the screen.

After getting this bit of your betting script working, you may decide you don't always want the message to print out that the user has won $1,000; you might want to hold the value in a variable and print that out. If the variable was called "value" we could set it as follows:

 value=500 

But we have a problem when we try to print the contents of $value with a "$" in front of it. If we try the following:

 echo You have won $$value 

We will get:

 You have won 1421value 

or something similar because the environment variable $$ holds the process ID of the current shell. The problem we have is that we want the shell to see the "$" of $value, but not the other one.

There are two solutions. The first is to put single quotes around the first dollar sign:

 echo You have won '$'$value 

The second looks a bit neater and that is to put a backslash character (\) before the first dollar sign:

 echo You have won \$$value 

The backslash will hide the next character from the shell regardless of what it is.

In summary, we can use single quotes, double quotes, or the backslash to hide things from the shell, but double quotes are not as strong as the others are, since they do not hide variables from the shell. The reason that double quotes do not hide variables is deliberate and helps us get around a potential problem with the way the shell handles unassigned variables.

We saw above that if a variable has no value, the shell simply removes it from the command line. So if the variable $firstname has no value but $lastname is set to Butler, the following would happen:

 $ echo My name is $firstname $lastname My name is Butler $ 

This is not such a problem when we are just printing something to the screen, but if we wanted to pass parameters to a command that expects a certain number we could have a problem.

If a program called name expected to be called with two parameters we could call it as follows:

 $ name $firstname $lastname $ 

But if $firstname was not set the shell would remove it from the command line and the program called name would complain that it had only been called with one parameter rather than the expected two. The way to fix this is to put double quotes around each of the variable names.

 $ name "$firstname" "$lastname" $ 

Even though the empty variable is removed from the line, the now empty first set of double quotes remains and is treated as an empty parameter by the program.

There are in fact a whole selection of characters that have a special meaning to the shell (the correct term for them is "shell metacharacters") and all can be hidden from the shell if required. Single quotes will hide everything they enclose, double quotes will hide everything apart from the dollar symbol, and a backslash will hide the next single character, whatever it is.


    Team-Fly    
    Top
     



    Solaris Operating Environment Boot Camp
    Solaris Operating Environment Boot Camp
    ISBN: 0130342874
    EAN: 2147483647
    Year: 2002
    Pages: 301

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net