Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 the / (slash) using fish shell syntax.

Expected result

America

Bash equivalent

Using bash, I was simply using a parameter expansion:

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

Question

How do I do that in a fish-way?

like image 632
Édouard Lopez Avatar asked Dec 09 '15 20:12

Édouard Lopez


1 Answers

Since fish 2.3.0, there's a builtin called string that has several subcommands including replace, so you'll be able to do

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

or

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

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

Alternatively, external tools like cut, sed or awk all work as well.

like image 150
faho Avatar answered Sep 20 '22 05:09

faho