在Mac中的命令bash文件中使用变量

时间:2020-09-27 14:50:58

标签: bash macos sh zsh oh-my-zsh

我创建了一个bash脚本,用于在Mac OS中显示通知5秒。

#!/bin/bash

arr[0]="0"
arr[1]="1"
arr[2]="2"
arr[3]="3"
arr[4]="4"
arr[5]="5"
arr[6]="6"

while true; do
 
    rand=$[$RANDOM % ${#arr[@]}]
    text=${arr[$rand]}
    
    osascript -e 'display notification '"$text"' with title "hello"'

    sleep 5
done

但显示带有标题“ hello”和描述$ text的通知。 如何在此命令中使用$ text变量?

1 个答案:

答案 0 :(得分:4)

"$text"的值作为参数传递给静态脚本。

osascript -e 'on run argv' \
          -e 'display notification (item 1 of argv) with title "hello"' \
          -e 'end run' "$text"

osascript -e 'on run argv
display notification (item 1 of argv) with title "hello"
end run' "$text"

顺便说一句,$[...]是一种非常古老且过时的算术扩展形式。请改用$((...))