Jump to content

How to use variables in bash


Recommended Posts

Hi there, I have the following workflow, and I can get the global variable "bearer" (in green) to work, but using a variable defined in args and vars (in red), I can't seem to pass it through - Any ideas?

 

curl "https://beta.todoist.com/API/v8/tasks" \
    -X POST \
--data '{"project_id": 2187691249, "content": "{query}", "due_string": "$xyz", "due_lang": "en", "priority": 4}' \
    -H "Content-Type: application/json" \
    -H "X-Request-Id: $(uuidgen)" \
    -H "Authorization: Bearer $bearer"

 

Would be keen to find out what I'm doing wrong here, as I can't seem to get it to work

 

Edited by marginwalker
Link to comment

Hello @marginwalker,

 

When asking for help with a Workflow, please upload it somewhere as we can’t properly help you without access to it. Debugging can already be hard with access to the code, and you’re asking us to guess yours from a description. There are multiple places where your code or Workflow setup may be going wrong. Without looking at it we’re shooting in the dark.


Read the Reporting Problems with Workflows topic, as it gives a nice overview on how to build an effective report.

Link to comment
2 minutes ago, deanishe said:

Use double quotes and escape the other double quotes within them.

 

Like so: --data "{\"project_id\": 2187691249, \"content\": \"{query}\", \"due_string\": \"$xyz\", \"due_lang\": \"en\", \"priority\": 4}"

 

19 minutes ago, marginwalker said:

Workaround found - Exported to a text file, and then ran bash file.txt

 

That makes no sense. There’s no reason why that would work.

 

Also, for bash files the typical extension is sh.

Link to comment

@vitor Thanks for that. I tried it your way - Unfortunately, the input needs single quotes, so escaping just isn't working. I'm getting errors every way i try. 

 

Instead what I did was build out the query with the variables using "write text file", and run it using bash. I'm not sure why you're upset that it works, but it does. 

Link to comment
3 hours ago, vitor said:

 

Like so: --data "{\"project_id\": 2187691249, \"content\": \"{query}\", \"due_string\": \"$xyz\", \"due_lang\": \"en\", \"priority\": 4}"

 

 

 

Nevermind - FINALLY got this to work... Thanks for the help...

 

However, I am curious - I actually quite like my workaround - What would be a reason not to use it? 

Link to comment

@deanishe 

 

I used the "write text file" functionality like this - ({clipboard}, {time}, {date} and {query} all ignore the single quote rules)

 

(My query was copied to the clipboard in the previous step

 

curl "https://beta.todoist.com/API/v8/tasks" \
    -X POST \
--data '{clipboard}' \
    -H "Content-Type: application/json" \
    -H "X-Request-Id: {time}{date}" \
    -H "Authorization: Bearer {var:bearer}"

 

The output is "task.txt"

 

Then I use bash task.txt 

 

It runs

 

 

Edited by marginwalker
Link to comment

Right, got you. You're using Alfred's macros, not shell variables.

 

13 minutes ago, marginwalker said:

{clipboard}, {time}, {date} and {query} all ignore the single quote rules

 

Naturally: That's a bash/shell rule. Those are Alfred macros. bash never even sees them.

Link to comment
9 hours ago, marginwalker said:

actually quite like my workaround - What would be a reason not to use it? 

 

8 hours ago, deanishe said:

It’s unnecessary.

 

And it’s slower and more resource intensive (it has to save a file and call it); it’s more complex (needs extra steps and extra Alfred nodes); and goes against convention (using a txt) making it harder for others (or you in the future) to debug.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...