Page 1 of 1

Using $1 in a shell script

PostPosted: Sun Dec 07, 2025 9:54 am
by ColCustard
Hello

I have a simple rule that detects a file in a folder and runs a shell script. I'm trying to get the found file to appear in a message posted by Pushover, it works but I can't find a way to include the file in the message part.

Here's the shell script, it uses curl

Code: Select all
## Request
curl -X "POST" "https://api.pushover.net/1/messages.json" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "token": "afgywqjuxxxxxxxxxxxxxxja23xc",
  "user": "usiwxxxxxxxxxxxhihdm25",
  "message": "\\${args[@]}"
}'


Anything I place in the message key/value appears in the notification, so at the moment it shows ${args[@]}, it's not correct and I have tried \\$1 and many other variations but am unable to get the file being processed.

Any help would be appreciated. Thanks.

Re: Using $1 in a shell script

PostPosted: Mon Dec 08, 2025 9:39 am
by Mr_Noodle
Why do you have the double backslashes preceding the variable?

Re: Using $1 in a shell script

PostPosted: Mon Dec 08, 2025 10:23 am
by ColCustard
Good question, it doesn’t work either way, with or without them, they never appear in the output but neither does the contents of the variable.

I’ve tried escaping the double quotes too but that causes an error, not in Hazel or its log but in the shell.

Re: Using $1 in a shell script

PostPosted: Tue Dec 09, 2025 2:03 pm
by Mr_Noodle
Oh, if you are using single quotes (') around the text, it won't do variable substitution. You'll need to use double-quotes and escape any double-quotes within.

Re: Using $1 in a shell script

PostPosted: Tue Dec 09, 2025 7:21 pm
by ColCustard
Thanks, the following rewrite worked

Code: Select all
#!/usr/bin/env zsh

TOKEN="xxx"
USER="yyy"

MESSAGE="$1"

curl -s \
  -X POST "https://api.pushover.net/1/messages.json" \
  -H "Content-Type: application/json; charset=utf-8" \
  -d "{
    \"token\": \"${TOKEN}\",
    \"user\": \"${USER}\",
    \"message\": \"${MESSAGE}\"
  }"