How to extract substring in fish shell?

I got the following variable:

set location "America/New_York" 

and want to display only the part before / (slash) using fish shell syntax .

Expected Result

America

Bash equivalent

Using bash, I just used the parameter extension:

 location="America/New_York" echo ${location##*/}" 

Question

How to do it in fish -way?

+11
source share
2 answers

Starting with fish 2.3.0, there is a built-in string that has several subcommands, including replace , so you can do

 string replace -r "/.*" "" -- $location 

or

 set location (string split "/" -- $location)[1] 

See https://fishshell.com/docs/current/commands.html#string .

In addition, external tools like cut , sed or awk work.

+14
source

A possible solution is to use cut , but look hacky:

 set location "America/New_York" echo $location|cut -d '/' -f1 

America

+3
source

All Articles