Determining if the first string starts with second string

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
JavaScript has a function for this:
'world'.startsWith('w')
true
How can I test this with shell? I have this code:
if [ world = w ]
then
echo true
else
echo false
fi
but it fails because it is testing for equality. I would prefer using a builtin,
but any utilities from this page would be acceptable:
http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
shell test
add a comment |Â
up vote
0
down vote
favorite
JavaScript has a function for this:
'world'.startsWith('w')
true
How can I test this with shell? I have this code:
if [ world = w ]
then
echo true
else
echo false
fi
but it fails because it is testing for equality. I would prefer using a builtin,
but any utilities from this page would be acceptable:
http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
shell test
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
JavaScript has a function for this:
'world'.startsWith('w')
true
How can I test this with shell? I have this code:
if [ world = w ]
then
echo true
else
echo false
fi
but it fails because it is testing for equality. I would prefer using a builtin,
but any utilities from this page would be acceptable:
http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
shell test
JavaScript has a function for this:
'world'.startsWith('w')
true
How can I test this with shell? I have this code:
if [ world = w ]
then
echo true
else
echo false
fi
but it fails because it is testing for equality. I would prefer using a builtin,
but any utilities from this page would be acceptable:
http://pubs.opengroup.org/onlinepubs/9699919799/idx/utilities.html
shell test
shell test
asked Aug 31 at 2:36
Steven Penny
2,31921635
2,31921635
add a comment |Â
add a comment |Â
2 Answers
2
active
oldest
votes
up vote
4
down vote
If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:
if [[ world == w* ]]; then
echo true
else
echo false
fi
Or more tersely: [[ world == w* ]] && echo true || echo false [*]
If you are not targetting bash specifically: use the case statement for pattern matching
case "world" in
w*) echo true ;;
*) echo false ;;
esac
[*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.
add a comment |Â
up vote
0
down vote
set world
Then:
if [ "$1%%w*" ]
then
echo false
else
echo true
fi
- Aggressively remove substring starting with
wfrom source string - If anything left, then source string does not start with second string
Or:
if [ "$1" = "$1#w" ]
then
echo false
else
echo true
fi
- Remove
wfrom source string - Compare with source string
- If equal, then source string does not start with second string
add a comment |Â
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:
if [[ world == w* ]]; then
echo true
else
echo false
fi
Or more tersely: [[ world == w* ]] && echo true || echo false [*]
If you are not targetting bash specifically: use the case statement for pattern matching
case "world" in
w*) echo true ;;
*) echo false ;;
esac
[*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.
add a comment |Â
up vote
4
down vote
If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:
if [[ world == w* ]]; then
echo true
else
echo false
fi
Or more tersely: [[ world == w* ]] && echo true || echo false [*]
If you are not targetting bash specifically: use the case statement for pattern matching
case "world" in
w*) echo true ;;
*) echo false ;;
esac
[*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.
add a comment |Â
up vote
4
down vote
up vote
4
down vote
If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:
if [[ world == w* ]]; then
echo true
else
echo false
fi
Or more tersely: [[ world == w* ]] && echo true || echo false [*]
If you are not targetting bash specifically: use the case statement for pattern matching
case "world" in
w*) echo true ;;
*) echo false ;;
esac
[*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.
If your shell is bash: within double brackets, the right-hand side of the == operator is a pattern unless fully quoted:
if [[ world == w* ]]; then
echo true
else
echo false
fi
Or more tersely: [[ world == w* ]] && echo true || echo false [*]
If you are not targetting bash specifically: use the case statement for pattern matching
case "world" in
w*) echo true ;;
*) echo false ;;
esac
[*] but you need to be careful with the A && B || C form because C will be executed if either A fails or B fails. The if A; then B; else C; fi form will only execute C if A fails.
answered Aug 31 at 2:51
glenn jackman
47.9k365105
47.9k365105
add a comment |Â
add a comment |Â
up vote
0
down vote
set world
Then:
if [ "$1%%w*" ]
then
echo false
else
echo true
fi
- Aggressively remove substring starting with
wfrom source string - If anything left, then source string does not start with second string
Or:
if [ "$1" = "$1#w" ]
then
echo false
else
echo true
fi
- Remove
wfrom source string - Compare with source string
- If equal, then source string does not start with second string
add a comment |Â
up vote
0
down vote
set world
Then:
if [ "$1%%w*" ]
then
echo false
else
echo true
fi
- Aggressively remove substring starting with
wfrom source string - If anything left, then source string does not start with second string
Or:
if [ "$1" = "$1#w" ]
then
echo false
else
echo true
fi
- Remove
wfrom source string - Compare with source string
- If equal, then source string does not start with second string
add a comment |Â
up vote
0
down vote
up vote
0
down vote
set world
Then:
if [ "$1%%w*" ]
then
echo false
else
echo true
fi
- Aggressively remove substring starting with
wfrom source string - If anything left, then source string does not start with second string
Or:
if [ "$1" = "$1#w" ]
then
echo false
else
echo true
fi
- Remove
wfrom source string - Compare with source string
- If equal, then source string does not start with second string
set world
Then:
if [ "$1%%w*" ]
then
echo false
else
echo true
fi
- Aggressively remove substring starting with
wfrom source string - If anything left, then source string does not start with second string
Or:
if [ "$1" = "$1#w" ]
then
echo false
else
echo true
fi
- Remove
wfrom source string - Compare with source string
- If equal, then source string does not start with second string
edited Aug 31 at 4:54
answered Aug 31 at 3:07
Steven Penny
2,31921635
2,31921635
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f465903%2fdetermining-if-the-first-string-starts-with-second-string%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password