Why isn't a read variable being found in my bash script? [duplicate]












0
















This question already has an answer here:




  • Why should there be a space after '[' and before ']' in Bash?

    4 answers



  • if statement error in bash [duplicate]

    2 answers




I'm not sure why I keep getting a not found error for a variable read from the console. I'm trying to test if the variable read (var) is equal to "q" or not with the if-then-else statement.



I have tried making "q" a variable (as it is now) and leaving it as a string inside the control structure. Both yielded the same result.



    q="q"

read var

echo "var = $var"
echo "q = $q"

if ["$var" == "$q"]
then
echo "if completed"
else
echo "else complete"
fi


If I type in "q" I want "if completed" to be output but I keep getting:



    "/home/adamgeds/Projects/bash.sh: 13: /home/adamgeds/Projects/bash.sh: [q: not found

else complete"


(I should also mention that I believe the "q" in the error refers to $var and not $q... if that helps.)










share|improve this question















marked as duplicate by Cyrus bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 20:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Add a shebang and then paste your script there: shellcheck.net

    – Cyrus
    Dec 31 '18 at 20:50











  • Perfect thank you.

    – Z0S0
    Dec 31 '18 at 21:02
















0
















This question already has an answer here:




  • Why should there be a space after '[' and before ']' in Bash?

    4 answers



  • if statement error in bash [duplicate]

    2 answers




I'm not sure why I keep getting a not found error for a variable read from the console. I'm trying to test if the variable read (var) is equal to "q" or not with the if-then-else statement.



I have tried making "q" a variable (as it is now) and leaving it as a string inside the control structure. Both yielded the same result.



    q="q"

read var

echo "var = $var"
echo "q = $q"

if ["$var" == "$q"]
then
echo "if completed"
else
echo "else complete"
fi


If I type in "q" I want "if completed" to be output but I keep getting:



    "/home/adamgeds/Projects/bash.sh: 13: /home/adamgeds/Projects/bash.sh: [q: not found

else complete"


(I should also mention that I believe the "q" in the error refers to $var and not $q... if that helps.)










share|improve this question















marked as duplicate by Cyrus bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 20:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    Add a shebang and then paste your script there: shellcheck.net

    – Cyrus
    Dec 31 '18 at 20:50











  • Perfect thank you.

    – Z0S0
    Dec 31 '18 at 21:02














0












0








0









This question already has an answer here:




  • Why should there be a space after '[' and before ']' in Bash?

    4 answers



  • if statement error in bash [duplicate]

    2 answers




I'm not sure why I keep getting a not found error for a variable read from the console. I'm trying to test if the variable read (var) is equal to "q" or not with the if-then-else statement.



I have tried making "q" a variable (as it is now) and leaving it as a string inside the control structure. Both yielded the same result.



    q="q"

read var

echo "var = $var"
echo "q = $q"

if ["$var" == "$q"]
then
echo "if completed"
else
echo "else complete"
fi


If I type in "q" I want "if completed" to be output but I keep getting:



    "/home/adamgeds/Projects/bash.sh: 13: /home/adamgeds/Projects/bash.sh: [q: not found

else complete"


(I should also mention that I believe the "q" in the error refers to $var and not $q... if that helps.)










share|improve this question

















This question already has an answer here:




  • Why should there be a space after '[' and before ']' in Bash?

    4 answers



  • if statement error in bash [duplicate]

    2 answers




I'm not sure why I keep getting a not found error for a variable read from the console. I'm trying to test if the variable read (var) is equal to "q" or not with the if-then-else statement.



I have tried making "q" a variable (as it is now) and leaving it as a string inside the control structure. Both yielded the same result.



    q="q"

read var

echo "var = $var"
echo "q = $q"

if ["$var" == "$q"]
then
echo "if completed"
else
echo "else complete"
fi


If I type in "q" I want "if completed" to be output but I keep getting:



    "/home/adamgeds/Projects/bash.sh: 13: /home/adamgeds/Projects/bash.sh: [q: not found

else complete"


(I should also mention that I believe the "q" in the error refers to $var and not $q... if that helps.)





This question already has an answer here:




  • Why should there be a space after '[' and before ']' in Bash?

    4 answers



  • if statement error in bash [duplicate]

    2 answers








bash ubuntu






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 20:50









Flimzy

38.7k106597




38.7k106597










asked Dec 31 '18 at 20:49









Z0S0Z0S0

1




1




marked as duplicate by Cyrus bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 20:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Cyrus bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Dec 31 '18 at 20:52


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    Add a shebang and then paste your script there: shellcheck.net

    – Cyrus
    Dec 31 '18 at 20:50











  • Perfect thank you.

    – Z0S0
    Dec 31 '18 at 21:02














  • 1





    Add a shebang and then paste your script there: shellcheck.net

    – Cyrus
    Dec 31 '18 at 20:50











  • Perfect thank you.

    – Z0S0
    Dec 31 '18 at 21:02








1




1





Add a shebang and then paste your script there: shellcheck.net

– Cyrus
Dec 31 '18 at 20:50





Add a shebang and then paste your script there: shellcheck.net

– Cyrus
Dec 31 '18 at 20:50













Perfect thank you.

– Z0S0
Dec 31 '18 at 21:02





Perfect thank you.

– Z0S0
Dec 31 '18 at 21:02












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Mossoró

Error while reading .h5 file using the rhdf5 package in R

Pushsharp Apns notification error: 'InvalidToken'