Basic help

Hi ,

I need to know the difference between $((command)) and $(command) and $(($(command))).
"" and '' and ``.
I have tried searching the help files but cant able to find this.
Could you let me knoq about any document.

Thanks

You need to supply additional info on e.g. OS, shell, that you use. Assuming bash :

  • the first expression will try to apply "arithmetic expansion" and assume command to be a variable and expand it (presumedly to zero).
  • the second is a "command substitution" and expand to command's stdout in lieu of the command name.
  • the third construct will apply comand substitution and then arithmetic expansion, so make sure the command sub. will yield an integer value.
    All this can be found in the man page of your shell.

Spaces can visualize where the tokens end.
For example:

x=1; echo "$(( x + 1 ))"
echo "$( ls )"
echo "$(( $( wc -l </etc/passwd ) + 1 ))"

All echo arguments are in "quotes", where $var and $( ) and $(( )) are evaluated.
But not in 'ticks', for example

echo '$( ls )'

And see what an unquoted

echo $( ls )

does!
The "quotes" are mostly used in command arguments; they evaluate $ as shown but not special characters like * .
An assignment like

x="$(( x + 1 ))"

does not need quotes, because the right side is not a command argument.
For quotes within quotes, one can sometimes choose the different quote types.

echo 'a part with "double-quote" within single-quotes'
echo "a part with 'single-quote' within double-quotes"

Or use a \ single-character quoting, preferably outside the quoted strings, using a simple string concatenation.

echo 'a part with '\''single-quotes'\'' withing single-quotes'
echo "a part with "\""double-quotes"\"" withing single-quotes"

Most has already been said, but:

`command` and $(command) does basically the same. It executes "command" and uses the output of this command to complete the command line. "command" could either be a single command or even a series of commands, a pipeline, etc..

Example:

command1 $(command2)

will first execute "command2", then put its output onto the commandline and execute the resulting line. Suppose the output to be "hello world" the shell would try to execute

command1 "hello world"

after completing "command2". The mechanism is called "process substitution".

The first variant is deprecated, has a lot of shortcomings and should be avoided. Use the second variant instead whenever possible.

Only very old shells (the original Bourne shell for instance) support the first variant(called "backticks") only. Modern shells (ksh, ksh93, bash, ...) support both variants but the former only for backwards compatibility.

I hope this helps.

bakunin

@bakunin: Sorry to disagree, but above is called "command substitution". man bash :

Process substitution takes a different form: