Skip to main content
XIII
January 27, 2022
Question

[46] How can I use a newline character in a field value?

  • January 27, 2022
  • 6 replies
  • 97 views

In the past I created some multi-line fields for SSH keys after reading this tip on this forum.

I now want to update one of those keys using op item edit "K" 'private key=X\nY'.

However then op item get "K" --fields "private key" returns this:


X\nY

where I hoped to get


X
Y

(as before)

How can I do this using the (new) CLI?


1Password Version: CLI 2.0.0-beta.11
Extension Version: n/a
OS Version: macOS Big Sur 11.6.2

6 replies

XIII
XIIIAuthor
January 27, 2022

Ah, it does work when doing it in a script:


op item edit "K" "private key=-----BEGIN OPENSSH PRIVATE KEY-----
X
Y
-----END OPENSSH PRIVATE KEY-----"

Still would like to know how to do it on the command line though.

January 28, 2022

Hey @XIII, thank you for pointing this out to us!
Indeed, it seems that strings that are passed to op item edit aren't parsed at all, except for separating the field key from the field value. I am going to open up a bug ticket for this, and discuss it with my team during our next meeting. I will follow up on the thread with the due updates.
Have you noticed such unexpected behaviour in other parts of the CLI too?
Also, do you have any other suggestions, related to how op item edit should work? Would, for example, being able to pipe input into the command be helpful to you?
Thank you for trying out our CLI. We're looking forward to hearing more of your feedback!!

Best,
Horia

XIII
XIIIAuthor
January 28, 2022

Yes, piping would be helpful.

January 28, 2022

Hey @XIII ,

While we look into the issue, I've found a temporary workaround in setting field values to contain newlines via the CLI.

I understand my solution is not ideal as it is quite inconvenient, but it may serve you for the time being while we sort things out.

You can set the values into a temporary variable:

export VALUE_WITH_NEWLINES="A\nb\n3"

And interpolate the variable into the op item edit or op item create commands:

op item edit exampeItem "section1.field1[text]=$(echo $VALUE_WITH_NEWLINES)"

By the way, I noticed you wrote "(as before)" in your original post, I am wondering if newlines had worked for you in setting field values in the CLI v1? I ask because I have not been able to do so, for example:

op1 create item login --vault demo --title exampleItem notesPlain="A\nb\n3"

Results in a username field that also contains the raw \n chars instead of newlines.

I share Horia's sentiment - thank you for the feedback!

XIII
XIIIAuthor
January 28, 2022

About the "before": I created some SSH "templates" and filled them with multiline text quite some time ago. I'm pretty sure I did that using the CLI (v1), though I can't remember the details...

January 31, 2022

Hey @XIII ,

Gotcha, I think templates would have worked with newlines in v1. I simply tried with the field assignments.

While templates continue to work in v2, the template body is not quite intuitive at the moment as it uses v1 item json body structures, and we are working on improving it!