Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you `split` fish shell variables as cmd line args

Tags:

fish

Is it possible to have fishshell split variables that are in cmd line arguments?

Assume I have a variable $args set like so:

$ set args "-a args"

Now, given this python program (test.py):

import sys
print(sys.argv)

If I run the above in fishshell I get this output:

$ python test.py $args
['test.py', '-a args']

Notice that the arguments are passed as one argument. When I do the equivalent in bash I get this output:

$ python test.py $args
['test.py', '-a', 'params']

Is there someway to make fish behave like bash?

like image 407
jaime Avatar asked Oct 19 '25 04:10

jaime


1 Answers

You do not want fish to behave like bash (technically any POSIX compatible shell) with respect to variable expansion. The POSIX behavior is the source of endless problems and is why you need to put double-quotes around almost everything. In fact, most experienced people will tell you to add IFS=$'\n' at the top of your scripts to stop that auto-splitting from happening.

One answer is to use fish's "every var is a list" feature: set args "-a" "args" (the quotes are just for clarity and aren't needed in this example). Each element of the list becomes a separate argument to the command. This will do the right thing even if the args value contains whitespace. The other answer is to explicitly split the string on whitespace using command substitution: a_cmd (string split ' ' $args). This will not do the right thing (in fish or bash) if the args value contains whitespace.

like image 122
Kurtis Rader Avatar answered Oct 20 '25 18:10

Kurtis Rader



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!