fish shell quickstart for converting bash scripts

After some years of bash and PowerShell, and some hours of using fish, I've realised that expansion & predictive typeahead are good features in a shell, whereas “be a great programming language” is less important than I thought: because there is no need to write scripts in the language of your shell.

Fish has slicker typeahead and expansions than bash or even PowerShell. But to switch to a fish shell, you do still have to convert your profile & start-up scripts. So here's my quick-start guide for converting bash to fish.

  • Do this first: at the fish prompt type help. Behold! the fish documentation in your browser is much easier to search than man pages are.
  • Calmly accept that fish uses set var value instead of var=value. Roll your eyes if it helps.
  • Use end everywhere that bash has fi, done, esac, braces {} etc. e.g. function definition is done with function ... end. The keywords do and then are redundant everywhere, just remove them. else has a semicolon after it. case requires a leading switch(expr).
  • There is no [[ condition ]] but [ ... ] or test ... work. Type help test to see all the file and numeric tests you expect, such as if [ -f filename ] etc. string and regex conditionals are done with the string match command (see below). You can replace [[ -f this && -z that || -z other ]] with [ -f this -a -z that -o -z other ] but see below for how fish can also replace || and && constructions with or and and statements.
  • But first! type help string to see the marvels of proper built-in string commands.
  • Replace function parameters $*, $1, $2 etc with $argv, $argv[1], $argv[2] etc. If that makes you scowl, then type help argparse. See! That's much better than kludging about in bash.
  • Remove the $ from $(subcommand) leaving just (subcommand). Inside quotes, take the subcommand outside the quote: "Today is $(date)" becomes "Today is "(date). (Recall that quotes in bash & fish don't work at all like quotes in most programming languages. Quote marks are not token delimiters and a"bc"d is a valid single token and is parsed identically to each of abcd , "abcd", abc'd').
  • Replace heredocs with multi-line literal strings and standard piping syntax. However, note that if you pipe or read to a variable, the default multiline behaviour is to split on newline and generate an array. Defeat this by piping through string split0 – see https://fishshell.com/docs/current/index.html#command-substitution

Search-and-replace Script Snippets

Here is my hit-list of things to search and replace to convert a bash shell to fish. These resolved almost all of my issues in converting a few hundred lines of bash script to fish.

FromToNotes
var=valueset var value
export var=valueset -x var value
export -f functionnameredundant.Just remove it
alias abbr='commandstring'(no change)alias syntax is accepted as an abbreviation for a function definition since fish 3
command $(subshell commmand)
command `subshell commmand`
command (subshell command)
OR
command (subshell commmand | string split0)
Just remove the $ but keep the ()

See below for when you want to add string split0
command "$(subshell commmand)"command (subshell command)Remove both the $ and the quotes ""to make this work
if [[ condition ]] ; then this ; else that ; fiif [ condition ] ; this ; else ; that ; endSee below for more on Fish's multine and and or syntax.
if [[ number != number ]] ; then this ; else that ; fiif [ number -ne number ] ; this ; else ; that ; endSee below for more on Fish's multine and and or syntax.
while condition ; do something ; donewhile condition ; something ; end
$*$argv
$1, $2$argv[1], $argv[2]But see help argparse
if [[ testthis =~ substring ]] if string match -q '*substring*' testthisstring match without -r does glob style testing
if [[ testthis =~ regexpattern ]] if string match -rq regexpattern testthisstring match with -r does regex testing
[ guardcondition ] && command
[ guardcondition ] || command
works as isBut see or and and below for when it's more complex
var=${this:-$that}if set -q this ; set var $this ; else ; set var $that ; end
cat > outfile <<< "heredoc"
cat > outfile <<< "multiline … heredoc"
echo "multiline … heredoc" | cat > outfile no heredocs, but multiline strings are fine
NB printf is better than echo for anything complicated, in any shell.
if [[ -z $this && $that=~$pattern ]]if [ -z $this ] ; and string match -rq $pattern $that ;
content=$(curl $url)set content (curl $url | string split0)without the pipe to string split0, content will be split on newlines to an array of lines.

Fish's multine and and or syntax

Fish has a multiline and and or syntax that may be clearer than && and || in both conditionals and guarded commands. It is less terse.

[ condition ]
and do this
or do that

That said, && and || are still valid in commands :

[ condition ] && do this || do that

Other gotchas

  • You may have to read up on how fish does parameter expansion, and especially handling spaces, differently to bash.
  • Pipe & subcommand output to multiline strings or arrays: set x (cat myfile.txt) will set x to an array of the lines of myfile.txt. To keep x as a single multine string, use string split0 : set x (cat myfile.txt | string split0)

Official tips for new fishers:

See the FAQ at https://fishshell.com/docs/3.0/faq.html

Tower of Babel

Now—all the earth one language and one speech—and as they set off eastward they found a plain in the land of Shinar and became citizens in that place.

They said each to his neighbour 
Come! Let us make bricks and burn them with fire.
And bricks were for them stone,

and asphalt was for them mortar
And they said Come! Let us build for ourselves

a City-and-Tower
And its head in the heavens,
And let us make a name for ourselves
Lest we be scattered on the face of the earth.
…
     And the LORD came down to see 
      the city and the tower 
      which the sons of man built …
And the Lord said, “See, one people one language, all of this, and this their start of work
And nothing will be impossible for them,

all they plan they will do
Come, let us go down to that place

and mix up their language
That they will not hear,

each the speech of his neighbour
So the Lord scattered them from there

over all the earth,
and they stopped building the city.

That is why it was called Babel, because there the Lord confused the language of the whole world and from there the Lord scattered them over the face of the whole earth.” – Genesis 11.

Main structure

The so-called “chiastic” or crossover structure—in which the second half of a story or section (or even a single sentence) mirrors & reverses the first half, is a common structure in the bible's literature.

The structure exposes the themes. The opening and closing sentences tell us the theme of the story and when you contrast the opening with the close you see how the turning point — often the exact middle sentence — has changed things.

The rest provides the detail. Comparing the detail of the first half with the detail of the last half shows what has changed in the light of the central turning point.

Reversals abound. The united language is disunited. The settling together is reversed by scattering. They want to build up to heaven, but instead God comes down from heaven. They want to make a name for themselves but instead are confused.

Less obvious is the importance of the place name. Babel is not named at the beginning because it serves as a pun for “Balel”—to confuse—at the end, which is appropriate after the turning point, not before it. Before then it is referred to as 'that place' in Shinar.

Second Structure

As well as this main structure, there is a second parallel structure between the two halves. The parallels rest as much on the words and sounds as the meaning, and again v5 is the mid-point:

v1
One language One Speech
In That Place
They speak, each to his neighbour
Build a City and a Name
Lest We are Scattered over the face of the whole earth
v5
And the LORD came down to see the city and the tower which the sons of man built.
v6
One People One Language
In That Place
They cannot hear, each the speech of his neighbour
Stop Building the City, 'great' Babel
Scattered Over the face of the whole earth

In this structure we can see that the second half of story repeats, in the same order, the vocabulary of the first half.

Third Structure

The parallel structure can be folded one more time into a third, ‘anti-parallel’ structure:

v1
One language One Speech
  In That Place
    They speak, each to his neighbour
  Build a City
Lest We are Scattered over the face of the whole earth

v5
And the LORD came down to see the city and the tower which the sons of man built.

v6
One People One Language
  In That Place
   They cannot hear! each his neighbour
  Stop! Building the City
Scattered Over the face of the whole earth

The point here is that the first half of each parallel half is parallel-by-similarity (One language; one speech; in that place); but the second half is parallel-by-contrast. Each half is a mini-chiasm on the theme of unity vs scattering. The turning point inside each half is speech; successful in the first half but unsuccessful in the second half. In this structure, the them is speech (successful vs unsuccessful) whilst the place and the city serve as the examples of what might have been when people are united in speech.

Words

We mentioned that Babel is punned in Hebrew as Balel, to mix or confuse. Invisible in English is the more extended alliteration in the short speech v3-4 of the consonants n, b and l in the words for, come let us build, brick, stone. This same alliteration is picked up again in the pun in verse 7 & 9 on “let us confuse” (nbl), Babel (bbl) and “he confused” (bbl).
[Hebrew was first written with just consonants not vowels; the letters h & m in this section are mostly parts of grammar not vocabulary]

Archeology

You can see pictures of Babylonian & nearby towers on Wikipedia:

https://en.wikipedia.org/wiki/Ziggurat

The oldest surviving one is dated to about 3000BC but similar earlier structures have been suggested as early as 6000 BC.

Meaning

The genesis text seems easy enough to interpet: The towers were intended to reach, figuratively at least, to the heavens. The “name” in “Make a name for ourselves” should be understood as fame or reputation.

Polemics?

The genesis text suggests the idea of men reaching heaven and makes no mention of the polytheist religion of Babylon. On the other hand:
• Babylonian texts probably consider the name Babel to derive from “Gate of god”.
• Herodotus says the top of the ziggurat was a shrine for the dwelling of gods.
• The Enmerkar epic has the confusion of languages being due to Enki (a senior god) making mischief and suggests that in the future (possibly the past; interpretation is uncertain) the languages will be united again.
• The main 1st millenium temple to Marduk in Babylon was Esagil–"house with the uplifted head"—and was next to the (probably 2nd millenium) Etemnaki–“Temple of the Foundation of Heaven and Earth”.
• The Enuma Elish considers the Babylonian template to be the “a likeness on earth of what he has wrought in heaven”. Indeed it says it was built by the minor gods the Annunaki:

The Annunaki wielded the hoe
For one whole year they moulded its bricks.
When the second year arrived
they raised the head of Esagil, a replica of the Apsû.
They built the lofty ziggurat of the Apsû
and established its … as a dwelling for Anu, Enlil and Ea [3 of the main gods].

Politically, the Babylonian empire was a major power for much of the period from the time of Genesis 12 in the 2nd millenium BC through to Babylon's defeat by Persia in 539BC.

All of which raises the question whether early readers understood the story as a polemic against the power and religion of Babylon. Like all empires, Babylon thought itself the centre of the world and the divinely blessed pinnacle of humanity.

But the Genesis story mocks. The tower to the heavens is so small that God has to come down to see it. All-conqueroring empire-building Babylon was once defeated by a little trick of speech; the resurgent Babylonian empires of the readers' times should be taken no more seriously.

Readers with their eyes open will be well aware that the impressive structures of Babylon—look again at those pictures on Wikipedia—were, like the monumental architecture of every other empire in history, built on the back of slaves,paid for by conquest, murder and theft. What is alleged to be the impressive demonstration of united humanity is in reality a testament to oppression & forced labour.

This is not the point made in the text however. Rather, the point made by God's interference is more, perhaps, the foolishness of human boasting? They who think themselves great achievers do not notice how contingent their achievements are. They who aspire to fame and monumental achievement should realise how futile those things are

References

• Enmerkar https://www.britannica.com/biography/Enmerkar
• Esagila https://en.wikipedia.org/wiki/Esagila
• Etemnaki https://en.wikipedia.org/wiki/Etemenanki
• Enuma Elish 6:59-64 : Full text at https://www.ancient.eu/article/225/enuma-elish---the-babylonian-epic-of-creation---fu/
• Babel as Gate of God: Wenham's Genesis commentary quoting Gelb, I. J. “The Name of Babylon.” Journal of the Institute of Asian Studies 1 (1955) 1-4

Simpson’s Paradox

Alice and Bob compete. Bob wins convincingly both times. But overall, Alice is better. How come?

This happens for real in medical trials and cases where you don't have much control over the size of groups you test on:


AliceBob
Trial 1 Score8010
–Out Of /100 /10
––Percentage => 80%=> 100%
Trial 2 Score240
–Out Of /10 /100
––Percentage=> 20%=> 40%
Total Score8250
–Total Trials /110 /110
––Percentage=> 75%=> 45%

The points to notice:

• In each trial, they got different sized groups assigned to them. There can be good reasons for this. One procedure may be known (or thought) to be better for specific circumstances so it would unethical to assign procedure based on “we want to simplify our statistical analysis” rather than on best possible outcome. Or you may simply be combining statistics about things not in your control.

• Alice's score for her largest group is better than Bob's score for his largest group, but those ‘largest groups’ were in different trials so they only ever get compared in the overall figure.

More on wikipedia: https://en.wikipedia.org/wiki/Simpson%27s_paradox.