Solved

Command explanation (newbie).

Posted on 2002-07-29
11
215 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
Comment Utility
: 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
Comment Utility
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
Comment Utility
it does nothing, you can remove it (in sh, ksh, csh, tcsh, but not bash)
some programmers just use it like a comment (#)
0
 
LVL 1

Author Comment

by:davism
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:davism
Comment Utility
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
Comment Utility
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
Comment Utility
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 50 total points
Comment Utility
> .. "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
Comment Utility
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
Comment Utility
even stupid questions get answered ;-)
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Linux users are sometimes dumbfounded by the severe lack of documentation on a topic. Sometimes, the documentation is copious, but other times, you end up with some obscure "it varies depending on your distribution" over and over when searching for …
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now