# Variable assignment, set the variable 'foo' to the
# value 'bar'. Fish doesn't use the = operator, since
# it is inherently whitespace sensitive. Also, the set
# command easily extends to work with arrays, scoping, etc.
> set foo bar
> echo $foo
bar
# Command substitution, assign the output of the command
# 'pwd' into the variable 'wd'. Fish doesn't use ``
# since they can't be nested and look too much like ' '.
> set wd (pwd)
> set wd $(pwd) # since version 3.4
> echo $wd
~
# Array variables. 'A' becomes an array with 5 values:
> set A 3 5 7 9 12
# Array slicing. 'B' becomes the first two elements of 'A':
> set B $A[1 2]
> echo $B
3 5
# You can index with other arrays and even command
# substitution output:
> echo $A[(seq 3)]
3 5 7
# Erase the third and fifth elements of 'A'
> set --erase A[$B]
> echo $A
3 5 9
# for-loop, convert jpegs to pngs
> for i in *.jpg
convert $i (basename $i .jpg).png
end
# Semicolons work like newlines:
> for i in *.jpg; convert $i (basename $i .jpg).png; end
# but the multi-line form is comfortable to use because
# fish supports multi-line history and editing.
# while-loop, read lines /etc/passwd and output the fifth
# colon-separated field from the file. This should be
# the user description.
> while read line
set arr (echo $line|tr : \n)
echo $arr[5]
end < /etc/passwd
# String replacement (replacing all i by I)
> string replace -a "i" "I" "Wikipedia"
WIkIpedIa