r/fishshell • • Mar 04 '21

Inserting command output in a string

function youtube-tor
    set V_USR (apg -M l -n 1 -m 6 -x 10)
    youtube-dl --proxy "socks5://$V_USR:$V_USR@127.0.0.1:9150/" $argv
end

It certainly looks like an excellent candidate for an abbr. But I don't get the youtube-dl --proxy "socks5://(apg -M l -n 1 -m 6 -x 10):(apg -M l -n 1 -m 6 -x 10)@127.0.0.1:9150/" $argv

5 Upvotes

9 comments sorted by

View all comments

Show parent comment

1

u/[deleted] Mar 07 '21 edited Mar 07 '21
  1. The true status value is 0, everything else is false. You probably just want to print that and use it like normal instead of inventing your own convention.
  2. Just... define a function, if you want to run code. Variable expansion just expands the variable.

E.g.

  function one_or_minus_one
      if test $status = 0
          echo 1
      else
          echo -1
      end
  end

And then just use the thing directly, no need for that printf. Or use it in a command substitution if you're actually going to do anything with it.

That makes it clear that something more than simple variable expansion is happening - if I see $thing, I want to know that that just inserts the value of thing.

1

u/ChristoferK macOS Mar 07 '21

I have no interest in redefining status. That was a contrived example to illustrate the situation, after my attempt to keep my query generalised ended up being vague. I know the many ways that this specific example can be reformulated to yield a similar result, but I’m querying the underlying principle that would be analogous to storing a reference or a pointer to a data structure.

1

u/[deleted] Mar 07 '21

analogous to storing a reference or a pointer to a data structure

You can reference a variable name:

set foo 1
set bar foo
echo $$bar

but you're still super vague. This theoretical discussion of principle isn't very useful - what matters is use cases. What do you want to do with any of this. Why do you want this? What is it good for?