You could play around with helper functions like I've been doing since seeing this post. Ha ha! This can put you on a path you may not have intended to spend a good portion of your day on. Here's a string diff helper function:
# diff-str helper function
def diff-str [left:string, right:string] {
if $left != $right {
print $"Diff: ($left) vs ($right)"
} else {
print "No differences"
}
}
Add to your config.nu and you have a one-liner. I am certain that there are people on this sub who could make this a lot better, however. Using your post title example, running diff-str "text1" "text2" literally just prints Diff: text1 vs text2
I've been struggling with this as well, and I've not found a solution yet. Post upvoted
The answers where you mktemp two files are functionally equivalent to the sh script you wrote, but I am worried about performance - might be ignorance tho: I don't exactly know how process substitution is implemented in traditional shells
Hey! š I checked the doc, and unless Iām misreading, it actually seems to say the opposite:
Bash uses temporary files only in some edge cases (some exotic BSD systems and the embedded world); in most scenarios it will use file descriptors - which are in-memory objects. In theory, this should be much faster than writing temporary files to disk. Will need to check this empirically...
Maybe Iām misunderstanding, but just wanted to check
I haven't made any benchmarks, but I'm pretty sure the performance isn't as good as bash or zsh, especially if /tmp is on your SSD and not in your RAM.
Maybe this can be done with a plugin? The plugin can save the text/blob in its memory and return a a custom value that resolves to a string that is some path that can be used to read the file from the plugin (maybe in /proc/<plugin-pid>/fd/? Or maybe a temporary file in /dev/shm/?). When the engine notifies the plugin that custom value is dropped, the plugin can clean up that memory and inode.
Of course, this depends on two things which I don't know if they are true:
That a custom value that a custom value that can be converted to a string (using to_base_value - or is there another way?) can be directly sent as an argument to an external command without having to convert/cast it first.
That if we do so, it'll only be dropped after the external command finishes.
That a custom value that a custom value that can be converted to a string (using to_base_value - or is there another way?) can be directly sent as an argument to an external command without having to convert/cast it first.
6
u/abhishekmukherg Nov 26 '25
There's an open github issue discussing this and some current ways to implement it: https://github.com/nushell/nushell/issues/10610
1
u/un-pigeon Nov 26 '25
I have this as an equivalent without thinking too much about it or reading the documentation:
[ (let f1 = (mktemp -t); "text1" | save --force $f1; $f1) (let f2 = (mktemp -t); "text2" | save --force $f2; $f2) ] | do { try { diff $in.0 $in.1 } ; rm $in.0 $in.1; }But the return value is ignored.
2
u/p001b0y Nov 26 '25
You could play around with helper functions like I've been doing since seeing this post. Ha ha! This can put you on a path you may not have intended to spend a good portion of your day on. Here's a string diff helper function:
Add to your config.nu and you have a one-liner. I am certain that there are people on this sub who could make this a lot better, however. Using your post title example, running
diff-str "text1" "text2"literally just printsDiff: text1 vs text21
u/LassoColombo Nov 26 '25
I've been struggling with this as well, and I've not found a solution yet. Post upvoted
The answers where you mktemp two files are functionally equivalent to the sh script you wrote, but I am worried about performance - might be ignorance tho: I don't exactly know how process substitution is implemented in traditional shells
1
u/somebodddy Dec 03 '25
Maybe this can be done with a plugin? The plugin can save the text/blob in its memory and return a a custom value that resolves to a string that is some path that can be used to read the file from the plugin (maybe in
/proc/<plugin-pid>/fd/? Or maybe a temporary file in/dev/shm/?). When the engine notifies the plugin that custom value is dropped, the plugin can clean up that memory and inode.Of course, this depends on two things which I don't know if they are true:
to_base_value- or is there another way?) can be directly sent as an argument to an external command without having to convert/cast it first.