Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access remaining arguments in a fish script

Tags:

fish

my-fish-script a b c d 

Say you want to get the all arguments from the second argument onwards, so b c d.

In bash you can use shift to dump the first argument and access the remaining ones with "$@".

How would you solve the problem using the fish shell?

like image 512
Dennis Avatar asked Jun 07 '14 04:06

Dennis


People also ask

How to run fish script?

To be able to run fish scripts from your terminal, you have to do two things. Add the following shebang line to the top of your script file: #!/usr/bin/env fish . Mark the file as executable using the following command: chmod +x <YOUR_FISH_SCRIPT_FILENAME> .

How many arguments can I pass to a bash script?

You can pass more than one argument to your bash script. In general, here is the syntax of passing multiple arguments to any bash script: script.sh arg1 arg2 arg3 … The second argument will be referenced by the $2 variable, the third argument is referenced by $3 , .. etc.

What is fish command?

Fish is a fully-equipped command line shell (like bash or zsh) that is smart and user-friendly. Fish supports powerful features like syntax highlighting, autosuggestions, and tab completions that just work, with nothing to learn or configure.

Where do you put a fish script?

Configuration¶ To store configuration write it to a file called ~/. config/fish/config.


1 Answers

In fish, your arguments are contained in the $argv list. Use list slicing to access a range of elements, e.g. $argv[2..-1] returns all arguments from the second to the last.

For example

function loop --description "loop <count> <command>"   for i in (seq 1 $argv[1])     eval $argv[2..-1]   end end 

Usage

$ loop 3 echo hello world hello world hello world hello world 
like image 161
Dennis Avatar answered Oct 19 '22 08:10

Dennis