2

What is the difference between expanding a variable and printing it (as with echo or printf)?

If I understand it correctly, printing a variable (its value), is just an example of expanding it. Maybe substituting its value is also an example.

Update

Please give a short definition of the term "variable expansion" in your own words, just before explaining the difference.

4 Answers4

8

Expanding and printing are two different actions. Expansion covers a number of phases in the shell’s processing of a command: in Bash, brace expansion ({1..5} becomes 1 2 3 4 5), tilde expansion (~user becomes /home/user as appropriate), shell parameter expansion (${variable} is replaced with the value of the variable), command substitution, arithmetic expansion, process substitution, word splitting, and filename expansion. (See also POSIX word expansion.)

One possible explanation for the use of the term expansion for all these is that they can all result in the command expanding, i.e. becoming longer (which is a particular concern when developing a shell in C).

In your case, the expansion is parameter expansion:

echo "${variable}"

becomes

echo "value"

after what you refer to as variable substitution, then

echo value

after quote removal (simplifying a little), and echo does the printing. It just so happens that echo and printf are shell built-ins, so only the shell is involved, but the steps are separate nevertheless, and the situaton would be identical with external commands.

So printing isn’t a special case of expansion; however, substitution (as in your linked question) is, see the Bash manual for details.

Stephen Kitt
  • 434,908
  • I tried to read in several cases about expanding variables and still didn't understand what this is and why someone need that. I can understand why someone needs to substitute a variable by its value, or, to just print that variable (say, with echo) but I still don't even start to understand what is "expanding" it and how it differs from the two actions I've just described (substituting or printing with echo). – Arcticooling Nov 10 '17 at 06:11
  • 1
    @Arcticooling the difference is that expansion is more general - it includes substitution. Sometimes you want to replace text in a variable and then substitute the new text. Also comes under expansion. Sometimes you want field splitting to take place. Still expansion. Sometimes you want to globbing to take place. Still expansion. Take this line from the bash manual, for instance: "The basic form of parameter expansion is ${parameter}. The value of parameter is substituted." – muru Nov 10 '17 at 06:39
  • Stephen, I now have a better understanding that variable expansion is a concept with many different private cases but I still miss what is the general logic that unites all these private cases or uses. In great plea I ask you to consider editing to explain that in your own words and your own rare and astonishing way to explain things for Bash newcomers like myself. I really tried to read about this but was confused, hence I think a general explanation of the logic there in general, in the start, plus code examples for at least some of the private cases you displayed, could be most helpful. – Arcticooling Nov 10 '17 at 10:34
  • 1
    @Arcticooling what are private cases? – muru Nov 10 '17 at 11:14
  • @muru I mean to different usages of this action called "expanding". The different things that can be done in the context of this action. The different outcomes you could get when expanding some variable. I still sadly miss to see the logical line or logical basis that ties them all together... The reason why it is called "expanding" and what is actually expanded. Any time I tried to read about it, I failed to understand that. Maybe I need a more didactic explanation and an edit in the start of the answer could help. – Arcticooling Nov 11 '17 at 01:10
  • 1
    @Arcticooling eh, don't think too much about it. It's called expansion, doesn't mean things are literally expanding. Somebody thought it was a good word, others agreed and the rest followed suit. As the saying goes, what's in a name? As for the different expansions available, you should read the manuals. If you dig too much, you can dig holes in the naming of anything. – muru Nov 11 '17 at 01:54
  • @muru, I'm not asking what are the other actions that falls under the term "expanding". I only ask what's being expanded and how it's different than printing the value of a variable. – Arcticooling Nov 11 '17 at 03:00
  • Moreover, I almost never came across a case a name was given irrationally. There should be a logic why the term "expansion" was given and what's exactly being expanded. – Arcticooling Nov 11 '17 at 03:51
  • @Arcticooling false dichotomy. A name could be rationally given and still not fit everything it is applied to. – muru Nov 11 '17 at 04:44
  • @muru, okay, I don't say it would fit all, but some main or common actions should fit, and I miss to see what and why. – Arcticooling Nov 11 '17 at 05:03
3

When we look at what our shells do, there is, for example:

  • waiting for user input
  • interpreting user input
  • acting on user input
  • communicating with other programs/processes

And, of course, there is

  • printing data to the screen

... to the terminal, actually. Anyway, the shells take action, so that the user can look at data.

Expanding a variable is what a shell does when it interprets the user's input. The shell recognises the user made a reference to a variable and continues to interpret the user's input as if the user input contained the content of the variable, rather than the reference. For instance, let there be a variable FOO which stores the value bar. The user types the statement echo $FOO. The shell starts to interpret this statement, recognises $FOO as a reference to the variable named FOO and proceeds to interpreting the statement as if the user had typed the statement echo bar.

Printing a variable is projecting its content in a manner that lets the user perceive, i.e. read, it. It is executing the actual task of processing the value stored in the variable so that it is transported to the user. In the example of echo $FOO, the variable FOOis first expanded and then, as a result of the expansion, the echo routine prints the content of FOO to the screen.

In a sense, printing a value is the opposite of expanding a variable. A variable is expanded when the shell is reading in data, to determine what to do. When a variable is printed, the shell is handing out data, to the user.

Bananguin
  • 7,984
1

In simple terms, expanding a variable substitutes in its value, which can be used as a part of any shell command or script.

echo and printf are specific commands that cause a string to be printed to stdout (usually the screen). That string can be an expanded shell variable, or it could be a string literal or the output of another command, or set of commands.

So, you can see, they are quite different: you can expand a variable without printing it, or you can print something that isn't an expanded variable.

Time4Tea
  • 2,366
0

Variable expansion is performed by the shell before executing the affected command. This is independend from the command, may it be echo, printf or anything else.

For example:

param='-ltr'
ls $param

expands to

ls -ltr

and this is the command being executed.

rexkogitans
  • 1,329