Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to use the syntax:

A=${B:-C}

where A is the variable, B is the value I'm trying to assign, C is the default value if B is null.

Now I want to replace B with the command nc -l 443, so if nc receive a string through port 443, it is assigned to the variable A, else A is set to default value. I wrote the command as:

A=${`nc -l 443`:-NULL}

But I get an error:

-bash: A=${`nc -l 443`:-NULL}: bad substitution

How can I achieve this?

share|improve this question
    
syntax "A=${B:-C}" is variable expansion in bash but function. Firstly you should assign result of function to variable, than use variable expansion – Costas yesterday

Nested substitution is not available in any modern Bourne-like shells except zsh:

$ print -rl -- ${$(echo):-C}
C
$ print -rl -- ${$(echo 1):-C}
1

In other shells:

A=$(nc -l 443)
A=${A:-C}
share|improve this answer

In Bash, the line

A=${B:-C}

will assign the value of the variable B to the variable A if B is set and not null. Otherwise the variable A will get the value C, i.e. the string containing the single character C.

What you might want to do:

B=$( some command )
A=${B:-C}

After this, $A will either be the output from some command, if it's not null, or the character C, if it is.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.