Create bash script with osascript and passed var

562 views Asked by At

I haven't really done much in the way of bash scripts before so any help would be appreciated!

I've added this function to my .bash_profile:

function test()
{
    osascript -e 'tell app "System Events" to display dialog "$1"'
    echo "my name is $1"
}

The second line works fine, however, the first line will just show $1 in the dialog. I have tried a few variations to that line as well as:

osascript <<EOD
  tell app "System Events" to display dialog "$1"
EOD

Is there a certain way I need to concatenate $1 on the osascript line, or is there a better way to do this?

1

There are 1 answers

1
Tom Fenech On BEST ANSWER

In order to expand the variable $1, it must be within double quotes. The outer quotes are single, which is preventing the expansion. Try this:

test() {
    osascript -e "tell app \"System Events\" to display dialog \"$1\""
    echo "my name is $1"
}

The inner double quotes are escaped, which means they will be passed correctly.

I have also removed the function keyword as it serves no useful purpose (except to make your script incompatible with other shells).