Variable with quotation marks “$()” The 2019 Stack Overflow Developer Survey Results Are...
Is it okay to consider publishing in my first year of PhD?
Worn-tile Scrabble
If my opponent casts Ultimate Price on my Phantasmal Bear, can I save it by casting Snap or Curfew?
How did passengers keep warm on sail ships?
Can a flute soloist sit?
Why are there uneven bright areas in this photo of black hole?
Is there a way to generate a uniformly distributed point on a sphere from a fixed amount of random real numbers?
How to quickly solve partial fractions equation?
Dropping list elements from nested list after evaluation
How to type a long/em dash `—`
For what reasons would an animal species NOT cross a *horizontal* land bridge?
What is the meaning of Triage in Cybersec world?
What is this sharp, curved notch on my knife for?
Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?
How come people say “Would of”?
Why does the nucleus not repel itself?
Why not take a picture of a closer black hole?
Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?
Pokemon Turn Based battle (Python)
Why doesn't UInt have a toDouble()?
Did the UK government pay "millions and millions of dollars" to try to snag Julian Assange?
What does Linus Torvalds mean when he says that Git "never ever" tracks a file?
Button changing its text & action. Good or terrible?
How can I add encounters in the Lost Mine of Phandelver campaign without giving PCs too much XP?
Variable with quotation marks “$()”
The 2019 Stack Overflow Developer Survey Results Are InWhat is the point of the bash Null-operator “:”, colon?gdialog cannot output variablefind does not work with my variablebash variable array names and getting valuesDefining and incrementing a variable in bashRename directory using a variableFor loop with variable incrementusing variable within quotation marksQuotation Mark-InceptionUsing bash in combination with psql, passing variable between themAssigned variable for pipeline is not working
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
Previously a member @Evan Chen, wrote this script :
#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done
I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.
Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?
To make a nested command ??
Thanks
bash scripts
New contributor
add a comment |
Previously a member @Evan Chen, wrote this script :
#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done
I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.
Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?
To make a nested command ??
Thanks
bash scripts
New contributor
add a comment |
Previously a member @Evan Chen, wrote this script :
#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done
I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.
Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?
To make a nested command ??
Thanks
bash scripts
New contributor
Previously a member @Evan Chen, wrote this script :
#!/bin/bash
while [ true ]
do
currentoutput="$(lsusb)"
if [ "$currentoutput" != "$lastoutput" ]
then
echo "" date and Time >> test.log
date +%x_r >> test.log
lastoutput="$(lsusb)"
lsusb >> test.log
fi
sleep 5
done
I'm a newbie, trying to learn fast and I got a question about the variable's quotation marks.
Put a variable between $(), I get it, but why the quotation marks are needed, even in the if statement ?
To make a nested command ??
Thanks
bash scripts
bash scripts
New contributor
New contributor
edited 39 mins ago
Shankhara
New contributor
asked 46 mins ago
ShankharaShankhara
62
62
New contributor
New contributor
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
In currentoutput="$(lsusb)"
lsusb is not a variable, it is a command. What this statement does, it executes lsusb
command and assigns its output to currentoutput
variable.
Older syntax for this was
currentoutput=`lsusb`
you can find it in many examples and scripts
To answer the other part of your question, if [ ]
is just how syntax for if
is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
add a comment |
The following runs the external command command
and returns its output.
"$(command)"
Without the brackets/parentheses, this would look for a variable instead of running a command:
"$variable"
As for the difference between $variable
and "$variable"
, this becomes relevant when $variable
contains spaces. When using "$variable"
, the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable
the contents of the variable may be expanded into an argument list of multiple arguments.
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
add a comment |
Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS
shell variable).
For example,
$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1
Here we define howmany
function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.
This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var
variable, touch $var
would create two files, but touch "$var"
just one.
On side note, the
while [ true ]
part can be
while true
[
is a command which evaluates its arguments, and [ whatever ]
is always true regardless of what is inside. By contrast, while true
uses the command true
which always returns success exit status (and that's exactly what while
loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use :
instead of true
See also:
- When is double-quoting necessary?
- What is the point of the bash null operator : (colon)?
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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
});
}
});
Shankhara is a new contributor. Be nice, and check out our Code of Conduct.
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%2faskubuntu.com%2fquestions%2f1133173%2fvariable-with-quotation-marks%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
In currentoutput="$(lsusb)"
lsusb is not a variable, it is a command. What this statement does, it executes lsusb
command and assigns its output to currentoutput
variable.
Older syntax for this was
currentoutput=`lsusb`
you can find it in many examples and scripts
To answer the other part of your question, if [ ]
is just how syntax for if
is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
add a comment |
In currentoutput="$(lsusb)"
lsusb is not a variable, it is a command. What this statement does, it executes lsusb
command and assigns its output to currentoutput
variable.
Older syntax for this was
currentoutput=`lsusb`
you can find it in many examples and scripts
To answer the other part of your question, if [ ]
is just how syntax for if
is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
add a comment |
In currentoutput="$(lsusb)"
lsusb is not a variable, it is a command. What this statement does, it executes lsusb
command and assigns its output to currentoutput
variable.
Older syntax for this was
currentoutput=`lsusb`
you can find it in many examples and scripts
To answer the other part of your question, if [ ]
is just how syntax for if
is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
In currentoutput="$(lsusb)"
lsusb is not a variable, it is a command. What this statement does, it executes lsusb
command and assigns its output to currentoutput
variable.
Older syntax for this was
currentoutput=`lsusb`
you can find it in many examples and scripts
To answer the other part of your question, if [ ]
is just how syntax for if
is defined in bash. See more in https://www.tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
answered 40 mins ago
marosgmarosg
46437
46437
add a comment |
add a comment |
The following runs the external command command
and returns its output.
"$(command)"
Without the brackets/parentheses, this would look for a variable instead of running a command:
"$variable"
As for the difference between $variable
and "$variable"
, this becomes relevant when $variable
contains spaces. When using "$variable"
, the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable
the contents of the variable may be expanded into an argument list of multiple arguments.
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
add a comment |
The following runs the external command command
and returns its output.
"$(command)"
Without the brackets/parentheses, this would look for a variable instead of running a command:
"$variable"
As for the difference between $variable
and "$variable"
, this becomes relevant when $variable
contains spaces. When using "$variable"
, the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable
the contents of the variable may be expanded into an argument list of multiple arguments.
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
add a comment |
The following runs the external command command
and returns its output.
"$(command)"
Without the brackets/parentheses, this would look for a variable instead of running a command:
"$variable"
As for the difference between $variable
and "$variable"
, this becomes relevant when $variable
contains spaces. When using "$variable"
, the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable
the contents of the variable may be expanded into an argument list of multiple arguments.
The following runs the external command command
and returns its output.
"$(command)"
Without the brackets/parentheses, this would look for a variable instead of running a command:
"$variable"
As for the difference between $variable
and "$variable"
, this becomes relevant when $variable
contains spaces. When using "$variable"
, the entire variable contents will be inserted into a single string even if the contents include spaces. When using $variable
the contents of the variable may be expanded into an argument list of multiple arguments.
edited 39 mins ago
answered 43 mins ago
thomasrutterthomasrutter
27.3k47089
27.3k47089
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
add a comment |
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !
– Shankhara
40 mins ago
add a comment |
Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS
shell variable).
For example,
$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1
Here we define howmany
function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.
This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var
variable, touch $var
would create two files, but touch "$var"
just one.
On side note, the
while [ true ]
part can be
while true
[
is a command which evaluates its arguments, and [ whatever ]
is always true regardless of what is inside. By contrast, while true
uses the command true
which always returns success exit status (and that's exactly what while
loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use :
instead of true
See also:
- When is double-quoting necessary?
- What is the point of the bash null operator : (colon)?
add a comment |
Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS
shell variable).
For example,
$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1
Here we define howmany
function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.
This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var
variable, touch $var
would create two files, but touch "$var"
just one.
On side note, the
while [ true ]
part can be
while true
[
is a command which evaluates its arguments, and [ whatever ]
is always true regardless of what is inside. By contrast, while true
uses the command true
which always returns success exit status (and that's exactly what while
loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use :
instead of true
See also:
- When is double-quoting necessary?
- What is the point of the bash null operator : (colon)?
add a comment |
Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS
shell variable).
For example,
$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1
Here we define howmany
function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.
This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var
variable, touch $var
would create two files, but touch "$var"
just one.
On side note, the
while [ true ]
part can be
while true
[
is a command which evaluates its arguments, and [ whatever ]
is always true regardless of what is inside. By contrast, while true
uses the command true
which always returns success exit status (and that's exactly what while
loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use :
instead of true
See also:
- When is double-quoting necessary?
- What is the point of the bash null operator : (colon)?
Quotation marks prevent word splitting - that is breaking down variables into multiple items at whitespaces (or to be more exact, at spaces, tabs, and newlines as defined in default value of $IFS
shell variable).
For example,
$ var="one two"
$ howmany(){ echo $#; }
$ howmany $var
2
$ howmany "$var"
1
Here we define howmany
function which just lets us know how many positional parameters are given. As you can see, there are two items being passed to the variable, and with the quotes the text in the variable is treated as one unit.
This is important for accurate passing of information. For example, if the variable contains path to file, and the filename contains spaces anywhere in the path, the command you are trying to run may fail or give inaccurate result. If we were trying to create file with $var
variable, touch $var
would create two files, but touch "$var"
just one.
On side note, the
while [ true ]
part can be
while true
[
is a command which evaluates its arguments, and [ whatever ]
is always true regardless of what is inside. By contrast, while true
uses the command true
which always returns success exit status (and that's exactly what while
loop needs). The difference is a bit more clarity and less testing performed. Alternatively, you could also use :
instead of true
See also:
- When is double-quoting necessary?
- What is the point of the bash null operator : (colon)?
edited 46 secs ago
answered 7 mins ago
Sergiy KolodyazhnyySergiy Kolodyazhnyy
75.1k9155327
75.1k9155327
add a comment |
add a comment |
Shankhara is a new contributor. Be nice, and check out our Code of Conduct.
Shankhara is a new contributor. Be nice, and check out our Code of Conduct.
Shankhara is a new contributor. Be nice, and check out our Code of Conduct.
Shankhara is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1133173%2fvariable-with-quotation-marks%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