0

Octave has an --eval option that takes a sting like "sqrt(4)" and outputs "ans = 2". I want to create a function that passes the a parameter as string so i can call it like this:

calc sqrt(4)

echivalent to:

octave --eval "sqrt(4)"

I wrote a simple function like this:

function calc --wraps octave
   octave --eval "$argv"
end

It does not work.

OUTPUT:

$calc sqrt(4)
fish: Unknown command: 4
in command substitution
fish: Unknown error while evaluating command substitution
calc sqrt(4)
2
  • "does not work" is a useless problem description: what does happen? Commented Apr 9, 2021 at 11:58
  • I have posted the output. Commented Apr 9, 2021 at 12:00

1 Answer 1

1

Parentheses are the fish syntax for command substitution. You have no choice but to escape the parentheses or quote the argument:

calc sqrt\(4\)
calc 'sqrt(4)'

In a simple case like this, you might prefer an abbreviation:

abbr -a calc 'octave --eval'

Then when you type calcspace, fish replaces that text with "octave --eval". (Still need to protect the parentheses in the argument though)

Can I make abbr to add " " automatically. So when i type calc Space i will apear 'octave --eval " " ' and my cursor will be in the quotes?

No, but you can do this with a key binding and a commandline function.

function calc_binding
    commandline -r 'octave --eval ""'
    commandline -C (math (commandline -C) - 1)
end

# bind Alt+c to that function
bind \ec calc_binding

When you hit Alt-C, the current command line is replaced by octave --eval "" and the cursor is placed one character back, before the ending double quote.

See bind --help and bind -a to see the current key bindings: think of a key sequence that's memorable for you, and bind -a will show if it's available.

2
  • Can I make abbr to add " " automatically. So when i type calc Space i will appear as 'octave --eval " " ' and my cursor will be in between the quotes? Commented Apr 9, 2021 at 12:21
  • Like adding a LeftArrow: 0x1B so it moves one letter left? Commented Apr 9, 2021 at 12:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .