Shell test to find a pattern in a stringString comparison in single brackets in zshSecurity implications of...

Should I be an author on another PhD student's paper if I went to their meetings and gave advice?

How to prove that the quadratic equation has exactly two real solutions

Why do Russians sometimes spell "жирный" (fatty) as "жырный"?

Airport Security - advanced check, 4th amendment breach

What are one's options when facing religious discrimination at the airport?

Is there anything on the ISS that would be destroyed if that object were returned to Earth?

How can Germany increase investments in Russia while EU economic sanctions against Russia are still in place?

Can I bring this power bank on board the aircraft?

What's the correct way to determine turn order in this situation?

What does "execute a hard copy" mean?

Could Boris Johnson face criminal charges for illegally proroguing Parliament?

Did Joe Biden "stop a prosecution" into his son in Ukraine? And did he brag about stopping the prosecution?

How to identify whether a publisher is genuine or not?

Missing quartile in boxplot

When Vesuvan Shapeshifter copies turn face up replacement effects, why do they work?

MaxCounters solution in C# from Codility

Would an object shot from earth fall into the sun?

Where does the image of a data connector as a sharp metal spike originate from?

Is there an in-universe explanation of how Frodo's arrival in Valinor was recorded in the Red Book?

Could the Queen overturn the UK Supreme Court ruling regarding prorogation of Parliament?

What is the point of impeaching Trump?

Everyone Gets a Window Seat

Isn't the detector always measuring, and thus always collapsing the state?

How deep is the liquid in a half-full hemisphere?



Shell test to find a pattern in a string


String comparison in single brackets in zshSecurity implications of forgetting to quote a variable in bash/POSIX shellsTest if a string contains a substringHow can I right trim a string with sed - and not kill the string if the pattern is not there?Array of string expanded to path?Get line number in a Bourne shell scriptare there any other shells than bash/dash which are used used as default in GNU/Linux distros?find my misunderstanding about this little piece of shell scriptHow/where does the shell store environment variables?bash scripts fail while trying to use listbash command to print string in unambiguous formString pattern-matching with =~POSIX shell: does `$` lose its special meaning if it is the last character in a word?






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








7















I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.



if [[ $line =~ "$PWD"$ ]] ;then









share|improve this question



























  • Did you try this? That command will work in modern bash shells just the way you typed it.

    – terdon
    Mar 27 '15 at 12:57











  • That won't work in dash.

    – Jonathan Leffler
    Mar 27 '15 at 14:06


















7















I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.



if [[ $line =~ "$PWD"$ ]] ;then









share|improve this question



























  • Did you try this? That command will work in modern bash shells just the way you typed it.

    – terdon
    Mar 27 '15 at 12:57











  • That won't work in dash.

    – Jonathan Leffler
    Mar 27 '15 at 14:06














7












7








7


1






I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.



if [[ $line =~ "$PWD"$ ]] ;then









share|improve this question
















I just wanted to ask whether there is any command which would work on common shells (bash, dash, kornshell)? It is supposed to check if the line variable contains any part of the path.



if [[ $line =~ "$PWD"$ ]] ;then






shell regular-expression string






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 27 '15 at 23:01









Gilles

577k140 gold badges1190 silver badges1702 bronze badges




577k140 gold badges1190 silver badges1702 bronze badges










asked Mar 27 '15 at 12:52









applenicapplenic

1813 gold badges4 silver badges13 bronze badges




1813 gold badges4 silver badges13 bronze badges
















  • Did you try this? That command will work in modern bash shells just the way you typed it.

    – terdon
    Mar 27 '15 at 12:57











  • That won't work in dash.

    – Jonathan Leffler
    Mar 27 '15 at 14:06



















  • Did you try this? That command will work in modern bash shells just the way you typed it.

    – terdon
    Mar 27 '15 at 12:57











  • That won't work in dash.

    – Jonathan Leffler
    Mar 27 '15 at 14:06

















Did you try this? That command will work in modern bash shells just the way you typed it.

– terdon
Mar 27 '15 at 12:57





Did you try this? That command will work in modern bash shells just the way you typed it.

– terdon
Mar 27 '15 at 12:57













That won't work in dash.

– Jonathan Leffler
Mar 27 '15 at 14:06





That won't work in dash.

– Jonathan Leffler
Mar 27 '15 at 14:06










2 Answers
2






active

oldest

votes


















6
















Yes, recent versions of bash can do this:



$ pwd
/home/terdon
$ line="I'm in /home/terdon"
$ [[ "$line" =~ "$PWD"$ ]] && echo yes
yes


The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities.



Note that regexp finds copied expansion $PWD as tail $anchored in literal copied expansion $line (maybe discounting 001 (start of transmission) but this editionist has not the time or derision to revisit that glaring omission to computational order since fixing printf for any rwongs in evidence).



similarly: <${line##*"$PWD"}>.||shite()?



if only the peace treaties and must be this high to fly could accredit to our visionary free thinking computer masterminds of self aggrandizing language defecations. but we could just party!



... ahem ...



hecking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in $line, remove the $:



$ line="I'm in /home/terdon, are you?"
$ [[ "$line" =~ "$PWD" ]] && echo yes
yes





share|improve this answer























  • 1





    No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

    – Stéphane Chazelas
    Mar 29 '15 at 21:34











  • @StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

    – terdon
    Mar 29 '15 at 22:51



















11
















In any POSIX-compatible shell you can do:



case $line in (*"$PWD"*)
# whatever your then block had
;;esac


This works in bash, dash, and just about any other shell you can name.



It can also be used to handle multiple possibilities easily. For example:



case $line in 
(*"$PWD"*)
echo $PWD match!
;;
(*"$OLDPWD"*)
echo $OLDPWD match!
;;
(*)
! echo no match!
;;esac


You can also use alternation:



case $line in (*"$PWD"*|*"$OLDPWD"*)
echo '$OLDPWD|$PWD match!'
;;esac


Note the use of quoting above:





  1. case $line ...

    • The object of a case statement will not be split on either $IFS or be used as a pattern for filename gen. This is similar to the way the left argument in a [[ test is treated.




  2. (*"$PWD"*)

    • Here also a shell expansion is not subjected to either $IFS or filename generation - an unquoted expansion will neither split nor glob.

    • But an unquoted expansion here might be construed as a pattern rather than a literal string though, and so an expansion might mean more than one thing depending on whether or not it is quoted.

    • It is important to quote any variable used in a pattern that should be literally interpreted, in the same way you would quote pattern chars which you wanted interpreted literally.

    • For example, if $PWD contained a * and was not quoted it would be construed as a pattern object and not as a literal * to be searched for.








share|improve this answer




























  • @terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

    – mikeserv
    Mar 29 '15 at 17:49






  • 2





    Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

    – Stéphane Chazelas
    Mar 29 '15 at 21:35













  • @StéphaneChazelas - true, but if you do that, zsh chokes on it.

    – mikeserv
    Mar 30 '15 at 8:53






  • 1





    What do you mean? Are your referring to things like $(case a in a);; esac)?

    – Stéphane Chazelas
    Mar 30 '15 at 10:22











  • @StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

    – mikeserv
    Mar 30 '15 at 10:32















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/4.0/"u003ecc by-sa 4.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%2f192887%2fshell-test-to-find-a-pattern-in-a-string%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









6
















Yes, recent versions of bash can do this:



$ pwd
/home/terdon
$ line="I'm in /home/terdon"
$ [[ "$line" =~ "$PWD"$ ]] && echo yes
yes


The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities.



Note that regexp finds copied expansion $PWD as tail $anchored in literal copied expansion $line (maybe discounting 001 (start of transmission) but this editionist has not the time or derision to revisit that glaring omission to computational order since fixing printf for any rwongs in evidence).



similarly: <${line##*"$PWD"}>.||shite()?



if only the peace treaties and must be this high to fly could accredit to our visionary free thinking computer masterminds of self aggrandizing language defecations. but we could just party!



... ahem ...



hecking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in $line, remove the $:



$ line="I'm in /home/terdon, are you?"
$ [[ "$line" =~ "$PWD" ]] && echo yes
yes





share|improve this answer























  • 1





    No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

    – Stéphane Chazelas
    Mar 29 '15 at 21:34











  • @StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

    – terdon
    Mar 29 '15 at 22:51
















6
















Yes, recent versions of bash can do this:



$ pwd
/home/terdon
$ line="I'm in /home/terdon"
$ [[ "$line" =~ "$PWD"$ ]] && echo yes
yes


The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities.



Note that regexp finds copied expansion $PWD as tail $anchored in literal copied expansion $line (maybe discounting 001 (start of transmission) but this editionist has not the time or derision to revisit that glaring omission to computational order since fixing printf for any rwongs in evidence).



similarly: <${line##*"$PWD"}>.||shite()?



if only the peace treaties and must be this high to fly could accredit to our visionary free thinking computer masterminds of self aggrandizing language defecations. but we could just party!



... ahem ...



hecking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in $line, remove the $:



$ line="I'm in /home/terdon, are you?"
$ [[ "$line" =~ "$PWD" ]] && echo yes
yes





share|improve this answer























  • 1





    No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

    – Stéphane Chazelas
    Mar 29 '15 at 21:34











  • @StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

    – terdon
    Mar 29 '15 at 22:51














6














6










6









Yes, recent versions of bash can do this:



$ pwd
/home/terdon
$ line="I'm in /home/terdon"
$ [[ "$line" =~ "$PWD"$ ]] && echo yes
yes


The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities.



Note that regexp finds copied expansion $PWD as tail $anchored in literal copied expansion $line (maybe discounting 001 (start of transmission) but this editionist has not the time or derision to revisit that glaring omission to computational order since fixing printf for any rwongs in evidence).



similarly: <${line##*"$PWD"}>.||shite()?



if only the peace treaties and must be this high to fly could accredit to our visionary free thinking computer masterminds of self aggrandizing language defecations. but we could just party!



... ahem ...



hecking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in $line, remove the $:



$ line="I'm in /home/terdon, are you?"
$ [[ "$line" =~ "$PWD" ]] && echo yes
yes





share|improve this answer















Yes, recent versions of bash can do this:



$ pwd
/home/terdon
$ line="I'm in /home/terdon"
$ [[ "$line" =~ "$PWD"$ ]] && echo yes
yes


The same syntax works in zsh and ksh but not in dash. As far as I know, dash has no such capabilities.



Note that regexp finds copied expansion $PWD as tail $anchored in literal copied expansion $line (maybe discounting 001 (start of transmission) but this editionist has not the time or derision to revisit that glaring omission to computational order since fixing printf for any rwongs in evidence).



similarly: <${line##*"$PWD"}>.||shite()?



if only the peace treaties and must be this high to fly could accredit to our visionary free thinking computer masterminds of self aggrandizing language defecations. but we could just party!



... ahem ...



hecking whether the variable $line ends with $PWD. To check if $PWD matches anywhere in $line, remove the $:



$ line="I'm in /home/terdon, are you?"
$ [[ "$line" =~ "$PWD" ]] && echo yes
yes






share|improve this answer














share|improve this answer



share|improve this answer








edited 20 mins ago









mikeserv

47.3k6 gold badges73 silver badges173 bronze badges




47.3k6 gold badges73 silver badges173 bronze badges










answered Mar 27 '15 at 12:57









terdonterdon

143k35 gold badges295 silver badges472 bronze badges




143k35 gold badges295 silver badges472 bronze badges











  • 1





    No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

    – Stéphane Chazelas
    Mar 29 '15 at 21:34











  • @StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

    – terdon
    Mar 29 '15 at 22:51














  • 1





    No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

    – Stéphane Chazelas
    Mar 29 '15 at 21:34











  • @StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

    – terdon
    Mar 29 '15 at 22:51








1




1





No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

– Stéphane Chazelas
Mar 29 '15 at 21:34





No, those only work in bash-3.2 and above provided the compat31 option has not been enabled or ksh93. It won't work properly in other bash versions or zsh if $PWD contains regular expression operators.

– Stéphane Chazelas
Mar 29 '15 at 21:34













@StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

– terdon
Mar 29 '15 at 22:51





@StéphaneChazelas yes, that's why I specified that recent versions of bash can do this. As for the rest, I just tried it in /home/terdon/foo with zsh, ksh and bash (4.3.25) and it worked fine with all of them. It does indeed break when PWD contains regex operators. That had simply never occurred to me, thanks!

– terdon
Mar 29 '15 at 22:51













11
















In any POSIX-compatible shell you can do:



case $line in (*"$PWD"*)
# whatever your then block had
;;esac


This works in bash, dash, and just about any other shell you can name.



It can also be used to handle multiple possibilities easily. For example:



case $line in 
(*"$PWD"*)
echo $PWD match!
;;
(*"$OLDPWD"*)
echo $OLDPWD match!
;;
(*)
! echo no match!
;;esac


You can also use alternation:



case $line in (*"$PWD"*|*"$OLDPWD"*)
echo '$OLDPWD|$PWD match!'
;;esac


Note the use of quoting above:





  1. case $line ...

    • The object of a case statement will not be split on either $IFS or be used as a pattern for filename gen. This is similar to the way the left argument in a [[ test is treated.




  2. (*"$PWD"*)

    • Here also a shell expansion is not subjected to either $IFS or filename generation - an unquoted expansion will neither split nor glob.

    • But an unquoted expansion here might be construed as a pattern rather than a literal string though, and so an expansion might mean more than one thing depending on whether or not it is quoted.

    • It is important to quote any variable used in a pattern that should be literally interpreted, in the same way you would quote pattern chars which you wanted interpreted literally.

    • For example, if $PWD contained a * and was not quoted it would be construed as a pattern object and not as a literal * to be searched for.








share|improve this answer




























  • @terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

    – mikeserv
    Mar 29 '15 at 17:49






  • 2





    Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

    – Stéphane Chazelas
    Mar 29 '15 at 21:35













  • @StéphaneChazelas - true, but if you do that, zsh chokes on it.

    – mikeserv
    Mar 30 '15 at 8:53






  • 1





    What do you mean? Are your referring to things like $(case a in a);; esac)?

    – Stéphane Chazelas
    Mar 30 '15 at 10:22











  • @StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

    – mikeserv
    Mar 30 '15 at 10:32


















11
















In any POSIX-compatible shell you can do:



case $line in (*"$PWD"*)
# whatever your then block had
;;esac


This works in bash, dash, and just about any other shell you can name.



It can also be used to handle multiple possibilities easily. For example:



case $line in 
(*"$PWD"*)
echo $PWD match!
;;
(*"$OLDPWD"*)
echo $OLDPWD match!
;;
(*)
! echo no match!
;;esac


You can also use alternation:



case $line in (*"$PWD"*|*"$OLDPWD"*)
echo '$OLDPWD|$PWD match!'
;;esac


Note the use of quoting above:





  1. case $line ...

    • The object of a case statement will not be split on either $IFS or be used as a pattern for filename gen. This is similar to the way the left argument in a [[ test is treated.




  2. (*"$PWD"*)

    • Here also a shell expansion is not subjected to either $IFS or filename generation - an unquoted expansion will neither split nor glob.

    • But an unquoted expansion here might be construed as a pattern rather than a literal string though, and so an expansion might mean more than one thing depending on whether or not it is quoted.

    • It is important to quote any variable used in a pattern that should be literally interpreted, in the same way you would quote pattern chars which you wanted interpreted literally.

    • For example, if $PWD contained a * and was not quoted it would be construed as a pattern object and not as a literal * to be searched for.








share|improve this answer




























  • @terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

    – mikeserv
    Mar 29 '15 at 17:49






  • 2





    Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

    – Stéphane Chazelas
    Mar 29 '15 at 21:35













  • @StéphaneChazelas - true, but if you do that, zsh chokes on it.

    – mikeserv
    Mar 30 '15 at 8:53






  • 1





    What do you mean? Are your referring to things like $(case a in a);; esac)?

    – Stéphane Chazelas
    Mar 30 '15 at 10:22











  • @StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

    – mikeserv
    Mar 30 '15 at 10:32
















11














11










11









In any POSIX-compatible shell you can do:



case $line in (*"$PWD"*)
# whatever your then block had
;;esac


This works in bash, dash, and just about any other shell you can name.



It can also be used to handle multiple possibilities easily. For example:



case $line in 
(*"$PWD"*)
echo $PWD match!
;;
(*"$OLDPWD"*)
echo $OLDPWD match!
;;
(*)
! echo no match!
;;esac


You can also use alternation:



case $line in (*"$PWD"*|*"$OLDPWD"*)
echo '$OLDPWD|$PWD match!'
;;esac


Note the use of quoting above:





  1. case $line ...

    • The object of a case statement will not be split on either $IFS or be used as a pattern for filename gen. This is similar to the way the left argument in a [[ test is treated.




  2. (*"$PWD"*)

    • Here also a shell expansion is not subjected to either $IFS or filename generation - an unquoted expansion will neither split nor glob.

    • But an unquoted expansion here might be construed as a pattern rather than a literal string though, and so an expansion might mean more than one thing depending on whether or not it is quoted.

    • It is important to quote any variable used in a pattern that should be literally interpreted, in the same way you would quote pattern chars which you wanted interpreted literally.

    • For example, if $PWD contained a * and was not quoted it would be construed as a pattern object and not as a literal * to be searched for.








share|improve this answer















In any POSIX-compatible shell you can do:



case $line in (*"$PWD"*)
# whatever your then block had
;;esac


This works in bash, dash, and just about any other shell you can name.



It can also be used to handle multiple possibilities easily. For example:



case $line in 
(*"$PWD"*)
echo $PWD match!
;;
(*"$OLDPWD"*)
echo $OLDPWD match!
;;
(*)
! echo no match!
;;esac


You can also use alternation:



case $line in (*"$PWD"*|*"$OLDPWD"*)
echo '$OLDPWD|$PWD match!'
;;esac


Note the use of quoting above:





  1. case $line ...

    • The object of a case statement will not be split on either $IFS or be used as a pattern for filename gen. This is similar to the way the left argument in a [[ test is treated.




  2. (*"$PWD"*)

    • Here also a shell expansion is not subjected to either $IFS or filename generation - an unquoted expansion will neither split nor glob.

    • But an unquoted expansion here might be construed as a pattern rather than a literal string though, and so an expansion might mean more than one thing depending on whether or not it is quoted.

    • It is important to quote any variable used in a pattern that should be literally interpreted, in the same way you would quote pattern chars which you wanted interpreted literally.

    • For example, if $PWD contained a * and was not quoted it would be construed as a pattern object and not as a literal * to be searched for.









share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 30 '15 at 10:48

























answered Mar 27 '15 at 23:09









mikeservmikeserv

47.3k6 gold badges73 silver badges173 bronze badges




47.3k6 gold badges73 silver badges173 bronze badges
















  • @terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

    – mikeserv
    Mar 29 '15 at 17:49






  • 2





    Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

    – Stéphane Chazelas
    Mar 29 '15 at 21:35













  • @StéphaneChazelas - true, but if you do that, zsh chokes on it.

    – mikeserv
    Mar 30 '15 at 8:53






  • 1





    What do you mean? Are your referring to things like $(case a in a);; esac)?

    – Stéphane Chazelas
    Mar 30 '15 at 10:22











  • @StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

    – mikeserv
    Mar 30 '15 at 10:32





















  • @terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

    – mikeserv
    Mar 29 '15 at 17:49






  • 2





    Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

    – Stéphane Chazelas
    Mar 29 '15 at 21:35













  • @StéphaneChazelas - true, but if you do that, zsh chokes on it.

    – mikeserv
    Mar 30 '15 at 8:53






  • 1





    What do you mean? Are your referring to things like $(case a in a);; esac)?

    – Stéphane Chazelas
    Mar 30 '15 at 10:22











  • @StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

    – mikeserv
    Mar 30 '15 at 10:32



















@terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

– mikeserv
Mar 29 '15 at 17:49





@terdon - you were right - the : comment was confusing, but I used it because it didn't gen a syntax error or accidentally run some proggie named whatever in $PATH. This way will still gen a syntax error, but at least it's kinda clear that it should.

– mikeserv
Mar 29 '15 at 17:49




2




2





Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

– Stéphane Chazelas
Mar 29 '15 at 21:35







Note that if you need portability with the Bourne shell (for some ancient systems for instance), you need to drop the (: case $line in *"$PWD"*)

– Stéphane Chazelas
Mar 29 '15 at 21:35















@StéphaneChazelas - true, but if you do that, zsh chokes on it.

– mikeserv
Mar 30 '15 at 8:53





@StéphaneChazelas - true, but if you do that, zsh chokes on it.

– mikeserv
Mar 30 '15 at 8:53




1




1





What do you mean? Are your referring to things like $(case a in a);; esac)?

– Stéphane Chazelas
Mar 30 '15 at 10:22





What do you mean? Are your referring to things like $(case a in a);; esac)?

– Stéphane Chazelas
Mar 30 '15 at 10:22













@StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

– mikeserv
Mar 30 '15 at 10:32







@StéphaneChazelas - I can't reproduce it, unless I do the command sub like in your example. It's weird too - zsh -cx '(case $1 in $2*) echo ok;;esac)' -- one o works, but prefixing a : $ breaks it. I guess that's the problem I encountered in the past - it was always a little unclear, and I never looked at it too hard because adding the leftmost ( always fixed it. Eventually I just wrote it off as some weird zsh thing and made it a rule to use both ends.

– mikeserv
Mar 30 '15 at 10:32





















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%2f192887%2fshell-test-to-find-a-pattern-in-a-string%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

Taj Mahal Inhaltsverzeichnis Aufbau | Geschichte | 350-Jahr-Feier | Heutige Bedeutung | Siehe auch |...

Baia Sprie Cuprins Etimologie | Istorie | Demografie | Politică și administrație | Arii naturale...

Ciclooctatetraenă Vezi și | Bibliografie | Meniu de navigare637866text4148569-500570979m