osascript using bash variable with a space
You could try to use instead :
osascript -e "display notification \"$var2\""
Or :
osascript -e 'display notification "'"$var2"'"'
This fixes the problem of manipulation of variables that contains spaces in bash. However, this solution doesn't protect against injections of osascript code. So it would be better to choose one of Charles Duffy's solutions or to use bash
parameter expansion :
# if you prefer escape the doubles quotes
osascript -e "display notification \"${var2//\"/\\\"}\""
# or
osascript -e 'display notification "'"${var2//\"/\\\"}"'"'
# if you prefer to remove the doubles quotes
osascript -e "display notification \"${var2//\"/}\""
# or
osascript -e 'display notification "'"${var2//\"/}"'"'
Thank to mklement0 for this very useful suggestion !
This version is completely safe against injection attacks, unlike variants trying to use string concatenation.
osascript \
-e "on run(argv)" \
-e "return display notification item 1 of argv" \
-e "end" \
-- "$var2"
...or, if one preferred to pass code in on stdin rather than argv:
osascript -- - "$var2" <<'EOF'
on run(argv)
return display notification item 1 of argv
end
EOF