1

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 Answer 1

3

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).

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, Tom! I don't know why I didn't think of just escaping them, but either way it works like a treat. Also, cheers for the heads up on the function keyword :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.