Reputation: 347
If I have a string var="root/Desktop"
, how can I determine whether var
var contains a '/'
character?
Upvotes: 2
Views: 219
Reputation: 72639
The portable solution that works in any Bourne-heritage shell and needs no expensive forks or pipes:
case $var in
(*/*) printf 'Has a slash.\n';;
(*) printf 'No slash.\n';;
esac
Upvotes: 3
Reputation: 20843
Bash can match against regular expressions with =~
, try:
[[ $var =~ "/" ]] && echo "contains a slash"
Upvotes: 8