?
Solved

Command explanation (newbie).

Posted on 2002-07-29
11
Medium Priority
?
224 Views
Last Modified: 2010-04-20
Hi,

Can someone please explain this Korn shell command?

: ${test:="/test"}

The thing that's throwing my for a loop is that first ":".  I don't understand that.

Any assistance would greatly be appreciated.

Thanks
0
Comment
Question by:davism
  • 6
  • 5
11 Comments
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7186495
: is the null command (no-op) in ksh
about the the parameter substitution: stolen from man ksh:

${name:=word}
       if name is set and not  null,  it  is  substituted,
       otherwise  it  is  assigned  word and the resulting
       value of name is substituted.
0
 
LVL 1

Author Comment

by:davism
ID: 7186539
But what does the null command mean?  Is it's a true null value then why have it there?  Does it do anything?  If it was absent would it change anything
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7186547
it does nothing, you can remove it (in sh, ksh, csh, tcsh, but not bash)
some programmers just use it like a comment (#)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:davism
ID: 7186558
What does it do in bash?

Also, does it actually do anything to any commands on the same line if it has a ":"?  So, for instance, in the command:

: ${test:="/test"}

In a ksh, with the ":" being there does the remainder of the line (i.e. ${test:="/test"}) actually do anything or it the entire line treated like a comment?
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7187301
as I said:
  the : command is a no-op (no operation), it does nothing, it does not execute anything, it is a "write-only" command, the shell will not do anything with it, the shell will ignore the complete line
DO YOU NEED MORE EXPLANATIONS?

That's different in bash (unfortunately), 'cause bash evaluates anything in the line before it realizes that it is a no-op.
0
 
LVL 1

Author Comment

by:davism
ID: 7188010
Yes, I would like a more detailed explanation.

In fact, from what I'm seeing IT DOES DO something.

: ${test:="/test"}

will still assign the literal "/test", IF $TEST is NOT ALREADY defined. And yes, I understand you had already mentioned that but this is the ":" comes in.  Without the ":", I would have a command like:

${test}:="/TEST"

I end up with:

/test: not found

With the ":", where I could have the command:
: ${test:="/test"}

Assuming the variable test, is not already defined then I end up with test being equal to "/test".

If it doesn't execute anything after the ":" (so in essence, a comment equivalent) then how is $test being equal to "/test"?  

Or is this what you are talking about with reference to bash shell?

Not trying to be a pain or argumentative, just looking for consise explanations to what I'm seeing (when I tried) it and what I'm hearing and reading.  I must be missing something here somewhere and I'm just trying to find out what that is. :-)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7188135
damn, checked Linux and its (pd)ksh man-pages where the : command is mentioned for the POSIX mode only.

From AT&T ksh man-page:

...
: [ arg ... ]
          The command only expands parameters.
...

so it does exactly what you described. It's a no-op, for any shell command but expands parameters.
Sorry for confusion.
0
 
LVL 1

Author Comment

by:davism
ID: 7188171
Terminology thing, I think, but when it says "expands parameters" isn't it actually executing the parameters where the parameter is a command or command substitution?

From what I'm gathering, it seems more like an execution but if the ":" is there it forcibly returns a success status (0 status) or a NULL execution (implying a success?)and that's why there is no message being displayed (i.e. it treats it as a successful command REGARDLESS)

Would that be a correct assessment?  If not, can you explain in "general" terms (remember, I'm a "newbie" when it comes to Unix) or any materials/books that you can recommend that can explain it in "general" terms with examples?

(haha...all this for a STUPID ":"...haha.)
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 7188204
> .. "expands parameters" isn't it actually executing the parameters ..
no.
parameters can be expanded, commands are executed

And yes, : always returns 0

> .. it treats it as a successful command ..
yes the : command (not the parameter expansion)

> .. correct assessment?
my english is to weak for such philosophical/pedantic/theoretical discussions ;-)
Sorry, don't have a referece handy.
0
 
LVL 1

Author Comment

by:davism
ID: 7188371
Great, thanks for that clarification it does make more sense, now. :-)  

Really appreciate the info and the time spent going over this...haha, again, all over a stupid ":".

Thanks again. :-)
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7188384
even stupid questions get answered ;-)
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month15 days, 6 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question