Home > OS >  variable expansion in echo and terminal
variable expansion in echo and terminal

Time:05-09

Bash version:

debian@debian:~$  bash --version |grep release
GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu)

Variable expansion in echo:

debian@debian:~$  a=apple
debian@debian:~$  echo '"'$a'
'
"apple

debian@debian:~$

I type '"'$a' and newline and ',echo parse it as three parts '"',$a,'newline',so it is ",apple,\n.What i analyse is the same as the output.Now remove the echo to see what happens:

Variable expansion in terminal:

debian@debian:~$  '"'$a'
> '
bash: $'"apple\n': command not found

Why terminal parse it as $'"apple\n'?

CodePudding user response:

When you hit enter to send a command line to the shell, it performs a variety of expansions (history, alias, parameter, etc), which produces a series of words. The name of the command to execute is the first word in that result that does not contain a = (or is not a predefined modifier like time).

In the first example, the first such word is echo, with the following word being the argument. The linefeed is written to the terminal, which "displays" it by moving the cursor to the beginning of the following line.

In the second example, the single word $'"apple\n' is the first such word. The $'...' is just how the shell displays a word with non-printable characters in its error message. It was decided that doing so would be clearer than

bash: "apple
: command not found
  • Related