Question

If I have a string var="root/Desktop", how can I determine whether var var contains a '/' character?

Was it helpful?

Solution

Bash can match against regular expressions with =~, try:

[[ $var =~ "/" ]] && echo "contains a slash"

OTHER TIPS

The following would work

[[ "$var" = */* ]]

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

echo "${var1}" | grep '/' should work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top