What does the slash do in 'case $1/$2' in shell script (/bin/sh)












3















Found this example on the arch wiki



#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac


and can't make out if it's a logical operator or something else.










share|improve this question

























  • it matches the / in pre/* and post/*

    – ctrl-alt-delor
    Dec 22 '18 at 17:46











  • It is a trick to test $1 and $2 at the same time.

    – Thorbjørn Ravn Andersen
    Dec 23 '18 at 1:04


















3















Found this example on the arch wiki



#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac


and can't make out if it's a logical operator or something else.










share|improve this question

























  • it matches the / in pre/* and post/*

    – ctrl-alt-delor
    Dec 22 '18 at 17:46











  • It is a trick to test $1 and $2 at the same time.

    – Thorbjørn Ravn Andersen
    Dec 23 '18 at 1:04
















3












3








3








Found this example on the arch wiki



#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac


and can't make out if it's a logical operator or something else.










share|improve this question
















Found this example on the arch wiki



#!/bin/sh
case $1/$2 in
pre/*)
echo "Going to $2..."
;;
post/*)
echo "Waking up from $2..."
;;
esac


and can't make out if it's a logical operator or something else.







shell scripting slash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 22 '18 at 17:45









ctrl-alt-delor

11.8k42260




11.8k42260










asked Dec 22 '18 at 16:22









James LanghamJames Langham

182




182













  • it matches the / in pre/* and post/*

    – ctrl-alt-delor
    Dec 22 '18 at 17:46











  • It is a trick to test $1 and $2 at the same time.

    – Thorbjørn Ravn Andersen
    Dec 23 '18 at 1:04





















  • it matches the / in pre/* and post/*

    – ctrl-alt-delor
    Dec 22 '18 at 17:46











  • It is a trick to test $1 and $2 at the same time.

    – Thorbjørn Ravn Andersen
    Dec 23 '18 at 1:04



















it matches the / in pre/* and post/*

– ctrl-alt-delor
Dec 22 '18 at 17:46





it matches the / in pre/* and post/*

– ctrl-alt-delor
Dec 22 '18 at 17:46













It is a trick to test $1 and $2 at the same time.

– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04







It is a trick to test $1 and $2 at the same time.

– Thorbjørn Ravn Andersen
Dec 23 '18 at 1:04












1 Answer
1






active

oldest

votes


















8














There is nothing special here. The syntax for case is



case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac


In the example in the question, word is built by combining $1, / and $2. / doesn't have a special meaning at all, it's just a character.



PS: Practically the usage is a bit strange, as the $2 part afterwards gets matched against * so the value of $2 doesn't really matter. One could, in the context described in the Wiki, also write



#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac


But there might be cases where suspend, hibernate or hybrid (the possible values for $2) are relevant, so it's just a general pattern here.






share|improve this answer





















  • 3





    maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

    – mosvy
    Dec 22 '18 at 17:19






  • 1





    @mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

    – nohillside
    Dec 22 '18 at 17:23











  • They would not be equivalent in cases where $1 is pre/something.

    – Stéphane Chazelas
    Dec 22 '18 at 17:58











Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "106"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f490491%2fwhat-does-the-slash-do-in-case-1-2-in-shell-script-bin-sh%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









8














There is nothing special here. The syntax for case is



case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac


In the example in the question, word is built by combining $1, / and $2. / doesn't have a special meaning at all, it's just a character.



PS: Practically the usage is a bit strange, as the $2 part afterwards gets matched against * so the value of $2 doesn't really matter. One could, in the context described in the Wiki, also write



#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac


But there might be cases where suspend, hibernate or hybrid (the possible values for $2) are relevant, so it's just a general pattern here.






share|improve this answer





















  • 3





    maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

    – mosvy
    Dec 22 '18 at 17:19






  • 1





    @mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

    – nohillside
    Dec 22 '18 at 17:23











  • They would not be equivalent in cases where $1 is pre/something.

    – Stéphane Chazelas
    Dec 22 '18 at 17:58
















8














There is nothing special here. The syntax for case is



case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac


In the example in the question, word is built by combining $1, / and $2. / doesn't have a special meaning at all, it's just a character.



PS: Practically the usage is a bit strange, as the $2 part afterwards gets matched against * so the value of $2 doesn't really matter. One could, in the context described in the Wiki, also write



#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac


But there might be cases where suspend, hibernate or hybrid (the possible values for $2) are relevant, so it's just a general pattern here.






share|improve this answer





















  • 3





    maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

    – mosvy
    Dec 22 '18 at 17:19






  • 1





    @mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

    – nohillside
    Dec 22 '18 at 17:23











  • They would not be equivalent in cases where $1 is pre/something.

    – Stéphane Chazelas
    Dec 22 '18 at 17:58














8












8








8







There is nothing special here. The syntax for case is



case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac


In the example in the question, word is built by combining $1, / and $2. / doesn't have a special meaning at all, it's just a character.



PS: Practically the usage is a bit strange, as the $2 part afterwards gets matched against * so the value of $2 doesn't really matter. One could, in the context described in the Wiki, also write



#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac


But there might be cases where suspend, hibernate or hybrid (the possible values for $2) are relevant, so it's just a general pattern here.






share|improve this answer















There is nothing special here. The syntax for case is



case word in [ [(] pattern [ | pattern ] ... ) list ;; ] ... esac


In the example in the question, word is built by combining $1, / and $2. / doesn't have a special meaning at all, it's just a character.



PS: Practically the usage is a bit strange, as the $2 part afterwards gets matched against * so the value of $2 doesn't really matter. One could, in the context described in the Wiki, also write



#!/bin/sh
case $1 in
pre)
echo "Going to $2..."
;;
post)
echo "Waking up from $2..."
;;
esac


But there might be cases where suspend, hibernate or hybrid (the possible values for $2) are relevant, so it's just a general pattern here.







share|improve this answer














share|improve this answer



share|improve this answer








edited Dec 22 '18 at 17:41

























answered Dec 22 '18 at 16:27









nohillsidenohillside

2,3901019




2,3901019








  • 3





    maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

    – mosvy
    Dec 22 '18 at 17:19






  • 1





    @mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

    – nohillside
    Dec 22 '18 at 17:23











  • They would not be equivalent in cases where $1 is pre/something.

    – Stéphane Chazelas
    Dec 22 '18 at 17:58














  • 3





    maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

    – mosvy
    Dec 22 '18 at 17:19






  • 1





    @mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

    – nohillside
    Dec 22 '18 at 17:23











  • They would not be equivalent in cases where $1 is pre/something.

    – Stéphane Chazelas
    Dec 22 '18 at 17:58








3




3





maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

– mosvy
Dec 22 '18 at 17:19





maybe $1 is pre/heat and $2 is oven in which case your code would not match, but the original would ;-)

– mosvy
Dec 22 '18 at 17:19




1




1





@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

– nohillside
Dec 22 '18 at 17:23





@mosvy True. But in the context of the script as explained on the Arch Wiki page linked in the question that's something which "should not happen". Haha.

– nohillside
Dec 22 '18 at 17:23













They would not be equivalent in cases where $1 is pre/something.

– Stéphane Chazelas
Dec 22 '18 at 17:58





They would not be equivalent in cases where $1 is pre/something.

– Stéphane Chazelas
Dec 22 '18 at 17:58


















draft saved

draft discarded




















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • 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%2funix.stackexchange.com%2fquestions%2f490491%2fwhat-does-the-slash-do-in-case-1-2-in-shell-script-bin-sh%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

Wiesbaden

Marschland

Dieringhausen