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?
shell zsh
add a comment |
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?
shell zsh
1
backticks are used (not POSIX) to run commands.
– Tico
Nov 23 '18 at 22:12
Here's zsh documentation: quotes, backticks. Also see One post with multiple questions or multiple posts? for why it's better to ask a single question per post.
– that other guy
Nov 24 '18 at 2:55
1
@Tico see POSIX Shell 2.6.3 Command Substitution
– David C. Rankin
Nov 24 '18 at 8:58
add a comment |
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?
shell zsh
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?
shell zsh
shell zsh
asked Nov 23 '18 at 22:08
MonsieurBeiltoMonsieurBeilto
406314
406314
1
backticks are used (not POSIX) to run commands.
– Tico
Nov 23 '18 at 22:12
Here's zsh documentation: quotes, backticks. Also see One post with multiple questions or multiple posts? for why it's better to ask a single question per post.
– that other guy
Nov 24 '18 at 2:55
1
@Tico see POSIX Shell 2.6.3 Command Substitution
– David C. Rankin
Nov 24 '18 at 8:58
add a comment |
1
backticks are used (not POSIX) to run commands.
– Tico
Nov 23 '18 at 22:12
Here's zsh documentation: quotes, backticks. Also see One post with multiple questions or multiple posts? for why it's better to ask a single question per post.
– that other guy
Nov 24 '18 at 2:55
1
@Tico see POSIX Shell 2.6.3 Command Substitution
– David C. Rankin
Nov 24 '18 at 8:58
1
1
backticks are used (not POSIX) to run commands.
– Tico
Nov 23 '18 at 22:12
backticks are used (not POSIX) to run commands.
– Tico
Nov 23 '18 at 22:12
Here's zsh documentation: quotes, backticks. Also see One post with multiple questions or multiple posts? for why it's better to ask a single question per post.
– that other guy
Nov 24 '18 at 2:55
Here's zsh documentation: quotes, backticks. Also see One post with multiple questions or multiple posts? for why it's better to ask a single question per post.
– that other guy
Nov 24 '18 at 2:55
1
1
@Tico see POSIX Shell 2.6.3 Command Substitution
– David C. Rankin
Nov 24 '18 at 8:58
@Tico see POSIX Shell 2.6.3 Command Substitution
– David C. Rankin
Nov 24 '18 at 8:58
add a comment |
1 Answer
1
active
oldest
votes
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`
:
- 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.
`echo test`
is a command substitution. (In modern shells this form is discouraged in favour of$(echo test)
.) This is processed as follows:
- The string inside the backticks (
echo test
) is word split into “echo” and “test”. - 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
?
- Standard error (in this case nothing) of the command is sent to the terminal as usual.
- 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.
- The string inside the backticks (
- The bytes above are assigned to the variable
one
.
$one
:
- This is a variable substitution. The bytes above are substituted for the variable, and the resulting string is treated as a command.
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. Seehelp test
orman test
for an in-depth explanation.
two='echo test'
:
- 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.
- The resulting set of bytes corresponding to the string “echo test” is assigned to the variable
two
.
$two
:
- 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'
) - 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.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453400%2fdifference-between-and-on-the-linux-terminal-zsh-shell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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`
:
- 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.
`echo test`
is a command substitution. (In modern shells this form is discouraged in favour of$(echo test)
.) This is processed as follows:
- The string inside the backticks (
echo test
) is word split into “echo” and “test”. - 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
?
- Standard error (in this case nothing) of the command is sent to the terminal as usual.
- 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.
- The string inside the backticks (
- The bytes above are assigned to the variable
one
.
$one
:
- This is a variable substitution. The bytes above are substituted for the variable, and the resulting string is treated as a command.
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. Seehelp test
orman test
for an in-depth explanation.
two='echo test'
:
- 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.
- The resulting set of bytes corresponding to the string “echo test” is assigned to the variable
two
.
$two
:
- 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'
) - 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.
add a comment |
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`
:
- 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.
`echo test`
is a command substitution. (In modern shells this form is discouraged in favour of$(echo test)
.) This is processed as follows:
- The string inside the backticks (
echo test
) is word split into “echo” and “test”. - 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
?
- Standard error (in this case nothing) of the command is sent to the terminal as usual.
- 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.
- The string inside the backticks (
- The bytes above are assigned to the variable
one
.
$one
:
- This is a variable substitution. The bytes above are substituted for the variable, and the resulting string is treated as a command.
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. Seehelp test
orman test
for an in-depth explanation.
two='echo test'
:
- 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.
- The resulting set of bytes corresponding to the string “echo test” is assigned to the variable
two
.
$two
:
- 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'
) - 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.
add a comment |
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`
:
- 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.
`echo test`
is a command substitution. (In modern shells this form is discouraged in favour of$(echo test)
.) This is processed as follows:
- The string inside the backticks (
echo test
) is word split into “echo” and “test”. - 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
?
- Standard error (in this case nothing) of the command is sent to the terminal as usual.
- 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.
- The string inside the backticks (
- The bytes above are assigned to the variable
one
.
$one
:
- This is a variable substitution. The bytes above are substituted for the variable, and the resulting string is treated as a command.
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. Seehelp test
orman test
for an in-depth explanation.
two='echo test'
:
- 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.
- The resulting set of bytes corresponding to the string “echo test” is assigned to the variable
two
.
$two
:
- 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'
) - 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.
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`
:
- 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.
`echo test`
is a command substitution. (In modern shells this form is discouraged in favour of$(echo test)
.) This is processed as follows:
- The string inside the backticks (
echo test
) is word split into “echo” and “test”. - 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
?
- Standard error (in this case nothing) of the command is sent to the terminal as usual.
- 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.
- The string inside the backticks (
- The bytes above are assigned to the variable
one
.
$one
:
- This is a variable substitution. The bytes above are substituted for the variable, and the resulting string is treated as a command.
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. Seehelp test
orman test
for an in-depth explanation.
two='echo test'
:
- 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.
- The resulting set of bytes corresponding to the string “echo test” is assigned to the variable
two
.
$two
:
- 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'
) - 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.
answered Nov 24 '18 at 5:20
l0b0l0b0
34.1k1585147
34.1k1585147
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53453400%2fdifference-between-and-on-the-linux-terminal-zsh-shell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
backticks are used (not POSIX) to run commands.
– Tico
Nov 23 '18 at 22:12
Here's zsh documentation: quotes, backticks. Also see One post with multiple questions or multiple posts? for why it's better to ask a single question per post.
– that other guy
Nov 24 '18 at 2:55
1
@Tico see POSIX Shell 2.6.3 Command Substitution
– David C. Rankin
Nov 24 '18 at 8:58