Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Expansion and Substitution seems to be interchangeble at same context in shell programming language. For example, some documents such as Bash reference manual, Bash Hackers Wiki use the word 'expansion' to explain 'shell parameter expansion'. However, some other documents seem to prefer the word 'substitution'. Advanced Bash-Scripting Guide uses parameter substituion.

Is there any difference between 'expansion' and 'substitution' in terms of shell programming terminology?

share|improve this question
    
Both are valid in different contexts, e.g. variable expansion, command substitution. What is your doubt? –  devnull Apr 16 at 6:29
    
Without specific context, I'm afraid that this is too broad and unclear too. –  devnull Apr 16 at 6:31
    
Looking at the bash man page, in general the word "expansion" seems to be used if only shell-internal stuff is involved, while "substitution" seems to be preferred for things involving external processes. –  celtschk Apr 16 at 6:32
    
If you'll take my advice, you'll quit reading those docs until you are completely familiar with these: pubs.opengroup.org/onlinepubs/9699919799/utilities/… When you're done with those you do man sh and man set. And when you've got that stuff down, then you do the shell-specific docs. –  mikeserv Apr 16 at 9:53
    
@devnull You are right. Although I put the link for the context, it would be better to specify the context explicitly with question. I will edit the question so that people can understand it clearly. –  MS.Kim Apr 16 at 16:39

2 Answers 2

Substitution is almost synonymous with expansion in this context because their meanings overlap. Neither is quite a complete sub category of the other, although in the GNU Manual section you reference there are substitutions that are considered as part of an overall expansion.

An expansion is extracting the value of an identifier. E.g., if this=that, when we expand this we get that. An expansion that doesn't involve substitution is pre-determined in that the value used already exists and must simply be retrieved, although this includes combining retrieved/explicit values (such as with "arithmetic expansion").

A substitution creates a value as the result of an operation. E.g., if this=$(foo bar), this is the result of passing bar to foo(). Although the value resulting from a substitution maybe completely predictable, it is different from the one retrieved in a normal expansion because it does not actually exist until the substitution takes place -- it is produced.

share|improve this answer
set -- arg arg2
echo ${2+"$1"}
 #OUTPUT 
arg

shift
echo ${2+"$1"}
 #OUTPUT

 #there doesn't seem to be anything here

I think the difference is generally too minimal to be worthy of notice - and the terms are often used interchangeably. Though, if you look at the above two cases, you can see that in the first example we substitute $1 for $2 as a result of the expansion of $2. As soon as $2 cannot be expanded there is no substitution.

goldilocks makes a good point about the ethereal existence of substitutions. Kind of like Schrodinger's cat, I guess. It reminded me of something. You may not be familiar with this form of POSIX-specified parameter expansion, but it works in a sort of opposite way to the above form:

${var:?if $var is unset or null its \
     parent shell dies and this message is output to stderr}

Now sometimes I want the same behavior but for a set value. POSIX doesn't specify that behavior for anything exactly. But, with a trick or two, it's simply managed:

N= #N is null
var="any value should fail"
${var:+${N:?we substitute our \$Null var when \$var is expanded}}
  #OUTPUT
sh: line 3: N: we substitute our $Null var when $var is expanded

But:

N= #N is null
var=
${var:+${N:?is never substituted}}
  #OUTPUT

#there doesn't seem to be anything here
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.