MonsieurBeilto
MonsieurBeilto

Reputation: 938

Difference between ' and ` on the linux terminal (zsh shell)

Using ' and ` leads to different results when setting a variable on the zsh shell script -

>>>one=`echo test`
>>>$one
>>>
>>>two='echo test'
>>>$two
>>>zsh: command not found: echo test

What the are the functions of the two?

Upvotes: 0

Views: 47

Answers (1)

l0b0
l0b0

Reputation: 58908

This is not as simple as it may seem. Here is a shallow explanation of what is happening. Depending on the shell (and version), variables such as IFS and other possibilities including at least aliases, any or all of the below may not apply, but I think it's a reasonable way of thinking about it in the normal case. Since I'm more familiar with Bash than Zsh I've included Bash references, but all this should apply to Zsh and other POSIX-ish shells as well.

Let's deconstruct these line by line.

one=`echo test`:

  1. Since the line starts with a valid variable name followed by an equals sign, it is a variable assignment, so the right side is processed first.
  2. `echo test` is a command substitution. (In modern shells this form is discouraged in favour of $(echo test).) This is processed as follows:
    1. The string inside the backticks (echo test) is word split into “echo” and “test”.
    2. The command consisting of the command “echo” is executed with an argument list consisting of a single item “test”. This is a rabbit hole, and I don't know the details, so I won't even try to explain what happens to actually execute this command. I think it has to do with execve?
      1. Standard error (in this case nothing) of the command is sent to the terminal as usual.
      2. Standard output of the command (in this case and this order, the bytes corresponding to the Basic Latin characters “t”, “e”, “s”, “t” and a newline) are then right trimmed of newlines, ending up with the bytes 0x74, 0x65, 0x73 and 0x74.
  3. The bytes above are assigned to the variable one.

$one:

  1. This is a variable substitution. The bytes above are substituted for the variable, and the resulting string is treated as a command.
  2. test is indeed a command (and very likely a zsh built-in as well), so it runs fine and returns with an exit code of 1 because there were no arguments. See help test or man test for an in-depth explanation.

two='echo test':

  1. Same as the other assignment, except the right side is a single quoted string. This means the entire contents between the two apostrophes is considered as a single literal string, and no word splitting occurs in it.
  2. The resulting set of bytes corresponding to the string “echo test” is assigned to the variable two.

$two:

  1. Like the other variable expansion, the entire string is treated as a command, because no word splitting happens before executing it. It would indeed be possible to create a command “echo test” (for example using ln -s /bin/echo '/bin/echo test')
  2. Since the command does not exist on your system (and indeed, on any other sane *nix system), zsh helpfully prints an error message to this effect.

Upvotes: 1

Related Questions