Variable with quotation marks “$()” Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)What is the point of the bash Null-operator “:”, colon?Why does bash remove n in $(cat file)?What other bash variables are available during execution such as $USER that can assist on my script?Problem with script substitution when running scriptChange Gsetting with script on LogoutHow to link a custom keyboard shortcut to a bash script in Ubuntu 13.04?Can you help me to understand this explanation of shell quoting?Installing breach browserhow to fix my keyboard after a bash script messed it up[Edited/include]man: bash. e and bash shell “enter” tokenusing variable within quotation marksHow to get BASH to use * wildcard in command?

Letter Boxed validator

Is a manifold-with-boundary with given interior and non-empty boundary essentially unique?

Examples of mediopassive verb constructions

If a contract sometimes uses the wrong name, is it still valid?

Best practices for giving outside developer SSH access?

Can a non-EU citizen traveling with me come with me through the EU passport line?

Why is black pepper both grey and black?

What do you call a plan that's an alternative plan in case your initial plan fails?

Why does Python start at index 1 when iterating an array backwards?

Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?

How to assign captions for two tables in LaTeX?

When to stop saving and start investing?

Is there a Spanish version of "dot your i's and cross your t's" that includes the letter 'ñ'?

Antler Helmet: Can it work?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

How to deal with a team lead who never gives me credit?

How to find all the available tools in macOS terminal?

What are the motives behind Cersei's orders given to Bronn?

What is this single-engine low-wing propeller plane?

Storing hydrofluoric acid before the invention of plastics

How can I make names more distinctive without making them longer?

Models of set theory where not every set can be linearly ordered

Do I really need recursive chmod to restrict access to a folder?

When -s is used with third person singular. What's its use in this context?



Variable with quotation marks “$()”



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)What is the point of the bash Null-operator “:”, colon?Why does bash remove n in $(cat file)?What other bash variables are available during execution such as $USER that can assist on my script?Problem with script substitution when running scriptChange Gsetting with script on LogoutHow to link a custom keyboard shortcut to a bash script in Ubuntu 13.04?Can you help me to understand this explanation of shell quoting?Installing breach browserhow to fix my keyboard after a bash script messed it up[Edited/include]man: bash. e and bash shell “enter” tokenusing variable within quotation marksHow to get BASH to use * wildcard in command?



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








12















I 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 are the quotation marks needed even in the if statement? Is it to make a nested command?










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    Apr 12 at 13:06






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    Apr 12 at 20:19

















12















I 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 are the quotation marks needed even in the if statement? Is it to make a nested command?










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    Apr 12 at 13:06






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    Apr 12 at 20:19













12












12








12


1






I 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 are the quotation marks needed even in the if statement? Is it to make a nested command?










share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I 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 are the quotation marks needed even in the if statement? Is it to make a nested command?







bash scripts






share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Apr 12 at 6:57









dessert

25.6k674108




25.6k674108






New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Apr 12 at 5:11









ShankharaShankhara

613




613




New contributor




Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Shankhara is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    Apr 12 at 13:06






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    Apr 12 at 20:19












  • 5





    Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

    – chepner
    Apr 12 at 13:06






  • 3





    You don't put a variable inside $(). You put a command inside $().

    – Wildcard
    Apr 12 at 20:19







5




5





Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

– chepner
Apr 12 at 13:06





Note that while [ true ] does produce an infinite loop, but perhaps not for the reason you think it does; while [ false ] also produces an infinite loop, because with a single argument, [ ... ] succeeds if that argument is a non-empty string. while true will actually run a command named true (which always succeeds).

– chepner
Apr 12 at 13:06




3




3





You don't put a variable inside $(). You put a command inside $().

– Wildcard
Apr 12 at 20:19





You don't put a variable inside $(). You put a command inside $().

– Wildcard
Apr 12 at 20:19










4 Answers
4






active

oldest

votes


















21














Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany() echo $#;
$ howmany $var
2
$ howmany "$var"
1


Here we define the 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 results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




On a 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



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




See also:



  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer

























  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    Apr 12 at 7:05







  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    Apr 12 at 13:26






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    Apr 12 at 16:13



















3














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






share|improve this answer


















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    Apr 12 at 9:08











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    Apr 12 at 16:16



















3














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.






share|improve this answer

























  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    Apr 12 at 5:17


















0














To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer























  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    Apr 12 at 13:01











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    Apr 12 at 14:36











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.









draft saved

draft discarded


















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

























4 Answers
4






active

oldest

votes








4 Answers
4






active

oldest

votes









active

oldest

votes






active

oldest

votes









21














Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany() echo $#;
$ howmany $var
2
$ howmany "$var"
1


Here we define the 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 results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




On a 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



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




See also:



  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer

























  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    Apr 12 at 7:05







  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    Apr 12 at 13:26






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    Apr 12 at 16:13
















21














Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany() echo $#;
$ howmany $var
2
$ howmany "$var"
1


Here we define the 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 results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




On a 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



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




See also:



  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer

























  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    Apr 12 at 7:05







  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    Apr 12 at 13:26






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    Apr 12 at 16:13














21












21








21







Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany() echo $#;
$ howmany $var
2
$ howmany "$var"
1


Here we define the 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 results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




On a 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



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




See also:



  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells





share|improve this answer















Quotation marks prevent "word splitting". That is: breaking down variables into multiple items at whitespace characters (or to be more exact, at spaces, tabs, and newlines as defined in the value of the default $IFS shell variable).



For example,



$ var="one two"
$ howmany() echo $#;
$ howmany $var
2
$ howmany "$var"
1


Here we define the 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 results. If we were trying to create a file with the $var variable, touch $var would create two files, but touch "$var" just one.



Same goes for your [ "$currentoutput" != "$lastoutput" ] part. This particular test performs a comparison on two strings. When the test runs, the [ command would need to see 3 arguments - a text string, the != operator, and another text string. Keeping double quotes prevents word splitting, and the [ command sees exactly those 3 arguments. Now what happens if variables are unquoted ?



$ var="hello world"
$ foo="hi world"
$ [ $var != $foo ]
bash: [: too many arguments
$


Here, word splitting occurs, and instead [ sees two strings hello and world followed by !=, followed by two other strings hi world. Key point is that without double quotes, the contents of variables are understood as separate units rather than one whole item.



Assigning command substitution doesn't require double quotes as in



var=$( df )


where you have the df command's output saved to var. However, it is a good habit to always double quote variables and command substitution $(...) unless you do in fact want the output to be treated as separate items.




On a 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



The double quotes in echo "" date and Time part could probably be removed. They merely insert an empty string and an add extra space to the output. If that's desired, feel free to keep them there, but there's no particular functional value in this case.



lsusb >> test.log


This part could probably be replaced with echo "$currentoutput" >> test.log. There's no reason to run lsusb again after it has been run already in currentoutput=$(lsusb). In cases where trailing newlines
have to be preserved in the output - one could see the value in running a command multiple times, but in case of lsusb there's no need for that. The less external commands you call, the better, because every call to a non-built-in command incurs costs in CPU, memory usage, and execution time ( even though the commands are probably pre-loaded from memory).




See also:



  • When is double-quoting necessary?

  • What is the point of the bash null operator : (colon)?

  • Security implications of forgetting to quote a variable in bash/POSIX shells






share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 12 at 10:57









terdon

67.8k13139223




67.8k13139223










answered Apr 12 at 5:50









Sergiy KolodyazhnyySergiy Kolodyazhnyy

75.5k9156330




75.5k9156330












  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    Apr 12 at 7:05







  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    Apr 12 at 13:26






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    Apr 12 at 16:13


















  • Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

    – RoVo
    Apr 12 at 7:05







  • 1





    @RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

    – Sergiy Kolodyazhnyy
    Apr 12 at 13:26






  • 1





    @SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

    – Charles Duffy
    Apr 12 at 16:13

















Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

– RoVo
Apr 12 at 7:05






Great answer, if you didn't already, you should write a book about bash. But in the last part shouldn't echo "$currentoutput" >> test.log better be printf '%sn' "$currentoutput" >> test.log ?

– RoVo
Apr 12 at 7:05





1




1





@RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

– Sergiy Kolodyazhnyy
Apr 12 at 13:26





@RoVo Yes, printf should be preferred for portability. Since we're using bash-specific script here it can be excused to use echo. But your observation is very much correct

– Sergiy Kolodyazhnyy
Apr 12 at 13:26




1




1





@SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

– Charles Duffy
Apr 12 at 16:13






@SergiyKolodyazhnyy, ...I'm not sure that echo is completely reliable even when the shell is known to be bash (and the value of the xpg_echo and posix flags is known); if your value could be -n or -e, it can disappear instead of being printed.

– Charles Duffy
Apr 12 at 16:13














3














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






share|improve this answer


















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    Apr 12 at 9:08











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    Apr 12 at 16:16
















3














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






share|improve this answer


















  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    Apr 12 at 9:08











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    Apr 12 at 16:16














3












3








3







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






share|improve this answer













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







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 12 at 5:17









marosgmarosg

47437




47437







  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    Apr 12 at 9:08











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    Apr 12 at 16:16













  • 3





    I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

    – Arronical
    Apr 12 at 9:08











  • ...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

    – Charles Duffy
    Apr 12 at 16:16








3




3





I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

– Arronical
Apr 12 at 9:08





I think it's important to say that [ ] is actually the test command. You can use if statements with other commands too, as it relies on a 0 or non-zero test of their exit code.

– Arronical
Apr 12 at 9:08













...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

– Charles Duffy
Apr 12 at 16:16






...indeed, it's much better to run if grep -qe "somestring" file than to run grep -qe "somestring" file; if [ $? = 0 ]; then ..., so the claim that if [ ... is part of the definition of if syntax is not just misleading but leads to bad practices.

– Charles Duffy
Apr 12 at 16:16












3














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.






share|improve this answer

























  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    Apr 12 at 5:17















3














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.






share|improve this answer

























  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    Apr 12 at 5:17













3












3








3







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.






share|improve this answer















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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 12 at 5:18

























answered Apr 12 at 5:14









thomasrutterthomasrutter

27.3k47089




27.3k47089












  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    Apr 12 at 5:17

















  • HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

    – Shankhara
    Apr 12 at 5:17
















HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

– Shankhara
Apr 12 at 5:17





HI @thomasrutter, I'm sorry, i meant quotation mark ... I edit my comment now !

– Shankhara
Apr 12 at 5:17











0














To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer























  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    Apr 12 at 13:01











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    Apr 12 at 14:36















0














To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer























  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    Apr 12 at 13:01











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    Apr 12 at 14:36













0












0








0







To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal






share|improve this answer













To be contrarian - bash recommends you use the [[ ... ]] over the [ ... ] construct to avoid having to quote variables in the test and so the associated problems of word splitting that the others have pointed out.



[ is provided in bash for POSIX compatibility with scripts meant to run under #!/bin/sh or those being ported over to bash - for the most part, you should avoid it in favour of [[.



e.g.



# With [ - quotes are needed

$ foo='one two'; bar='one two'; [ $foo = $bar ] && echo "they're equal"
-bash: [: too many arguments

$ foo='one two'; bar='one two'; [ "$foo" = "$bar" ] && echo "they're equal"
they're equal

# versus [[ - quotes not needed

$ foo='one two'; bar='one two'; [[ $foo = $bar ]] && echo "they're equal"
they're equal







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 12 at 12:56









shalombshalomb

1814




1814












  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    Apr 12 at 13:01











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    Apr 12 at 14:36

















  • bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

    – chepner
    Apr 12 at 13:01











  • @chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

    – shalomb
    Apr 12 at 14:36
















bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

– chepner
Apr 12 at 13:01





bash makes no such recommendation; it provides [[ ... ]] which can be more convenient and has some functionality that [ ... ] does not, but there is no problem with using [ ... ] correctly.

– chepner
Apr 12 at 13:01













@chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

– shalomb
Apr 12 at 14:36





@chepner - Maybe I should be clearer here. Sure it's not an explicit recommendation in the bash manpage but given [[ does everything [ does and more and still the many times [ trips people up and then try and retrofit features of [[ back to [ only to fail and leave bugs scattered about - it's fair to say it is a community recommendation in as far as most relevant bash style guides will advise never use [.

– shalomb
Apr 12 at 14:36










Shankhara is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Tamil (spriik) Luke uk diar | Nawigatjuun

Align equal signs while including text over equalitiesAMS align: left aligned text/math plus multicolumn alignmentMultiple alignmentsAligning equations in multiple placesNumbering and aligning an equation with multiple columnsHow to align one equation with another multline equationUsing \ in environments inside the begintabularxNumber equations and preserving alignment of equal signsHow can I align equations to the left and to the right?Double equation alignment problem within align enviromentAligned within align: Why are they right-aligned?

Where does the image of a data connector as a sharp metal spike originate from?Where does the concept of infected people turning into zombies only after death originate from?Where does the motif of a reanimated human head originate?Where did the notion that Dragons could speak originate?Where does the archetypal image of the 'Grey' alien come from?Where did the suffix '-Man' originate?Where does the notion of being injured or killed by an illusion originate?Where did the term “sophont” originate?Where does the trope of magic spells being driven by advanced technology originate from?Where did the term “the living impaired” originate?