Package: shell-functools
shell-functoolsA collection of functional programming tools for the shell.This project provides higher order functions likemap,filter,foldl,sort_byandtake_whileas simple command-line tools.
Following the UNIX philosophy, these commands are designed to be composed via pipes. Alarge collectionof functions such asbasename,replace,containsoris_dirare provided as
arguments to these commands.ContentsDemoQuick startDocumentation and examplesUsage ofmapUsage offilterUsage offoldlUsage offoldl1Usage ofsort_byChaining commandsLazy evaluationWorking with columnsAvailable function argumentsDemoQuick startIf you want to try it out on your own, run:pipinstallshell-functoolsIf you only want to try it out temporarily, you can also use:gitclonehttps://github.com/sharkdp/shell-functools/tmp/shell-functoolsexportPATH="$PATH:/tmp/shell-functools/ft"Documentation and examplesUsage ofmapThemapcommand takes afunction argumentand applies it to every line of input:>ls
document.txt
folder
image.jpg

>ls|mapabspath
/tmp/demo/document.txt
/tmp/demo/folder
/tmp/demo/image.jpgUsage offilterThefiltercommand takes afunction argumentwith aBoolean return type. It applies that function to each input line and shows only those that returnedtrue:>find
.
./folder
./folder/me.jpg
./folder/subdirectory
./folder/subdirectory/song.mp3
./document.txt
./image.jpg

>find|filteris_file
./folder/me.jpg
./folder/subdirectory/song.mp3
./document.txt
./image.jpgUsage offoldlThefoldlcommand takes afunction argumentand an initial value. The given function must be a binary function with two arguments, likeaddorappend. Thefoldlcommand then applies this function iteratively by keeping an internal accumulator:Add up the numbers from 0 to 100:>seq100|foldladd05050Multiply the numbers from 1 to 10:>seq10|foldlmul13628800Append the numbers from 1 to 10 in a string:>seq10|mapappend" "|foldlappend""12345678910Usage offoldl1Thefoldl1command is a variant offoldlthat uses the first input as the initial value.
This can be used to shorten the example above to:>seq100|foldl1add
>seq10|foldl1mul
>seq10|mapappend" "|foldl1appendUsage ofsort_byThesort_bycommand also takes afunction argument. In the
background, it calls the function on each input line and uses the results to sort theoriginal input.
Consider the following scenario:>ls
a.mp4b.tar.gzc.txt
>ls|mapfilesize76748601261382214We can use thefilesizefunction to sort the entries by size:> ls | sort_by filesize
c.txt
b.tar.gz
a.mp4Chaining commandsAll of these commands can be composed by using standard UNIX pipes:>find
.
./folder
./folder/me.jpg
./folder/subdirectory
./folder/subdirectory/song.mp3
./document.txt
./image.jpg

>find|filteris_file|mapbasename|mapappend".bak"me.jpg.bak
song.mp3.bak
document.txt.bak
image.jpg.bakLazy evaluationAll commands support lazy evaluation (i.e. they consume input in a streaming way) and never perform
unnecessary work (they exit early if theoutputpipe is closed).As an example, suppose we want to compute the sum of all odd squares lower than 10000. Assuming we
have a command that prints the numbers from 1 to infinity (usealias infinity="seq 999999999"for
an approximation), we can write:>infinity|filterodd|mappow2|take_whileless_than10000|foldl1add166650Working with columnsThe--column/-coption can be used to apply a given function to a certaincolumnin the input line (columns are separated by tabs). Column arrays can be created by using functions such asduplicate,split seporsplit_ext:>ls|filteris_file|mapsplit_ext
documenttxt
imagejpg

>ls|filteris_file|mapsplit_ext|map-c1to_upper
DOCUMENTtxt
IMAGEjpg

>ls|filteris_file|mapsplit_ext|map-c1to_upper|mapjoin.
DOCUMENT.txt
IMAGE.jpgHere is a more complicated example:>find-name'*.jpg'./folder/me.jpg
./image.jpg

>find-name'*.jpg'|mapduplicate
./folder/me.jpg./folder/me.jpg
./image.jpg./image.jpg

>find-name'*.jpg'|mapduplicate|map-c2basename
./folder/me.jpgme.jpg
./image.jpgimage.jpg

>find-name'*.jpg'|mapduplicate|map-c2basename|map-c2prepend"thumb_"./folder/me.jpgthumb_me.jpg
./image.jpgthumb_image.jpg

>find-name'*.jpg'|mapduplicate|map-c2basename|map-c2prepend"thumb_"|maprunconvert
Running'convert'witharguments['./folder/me.jpg','thumb_me.jpg']Running'convert'witharguments['./image.jpg','thumb_image.jpg']Get the login shell of usershark:>cat/etc/passwd|mapsplit:|filter-c1equalshark|mapindex6/usr/bin/zshAvailable function argumentsYou can callft-functions, to get an overview of all available arguments tomap,filter, etc.:File and Directory operationsabspath             :: Path   → Path
dirname             :: Path   → Path
basename            :: Path   → Path
is_dir              :: Path   → Bool
is_file             :: Path   → Bool
is_link             :: Path   → Bool
exists              :: Path   → Bool
has_ext ext         :: Path   → Bool
strip_ext           :: Path   → String
replace_ext new_ext :: Path   → Path
split_ext           :: Path   → ArrayLogical operationsnon_empty           :: *      → Bool
nonempty            :: *      → BoolArithmetic operationsadd num             :: Int    → Int
sub num             :: Int    → Int
mul num             :: Int    → Int
even                :: Int    → Bool
odd                 :: Int    → Bool
pow num             :: Int    → IntComparison operationseq other            :: *      → Bool
equal other         :: *      → Bool
equals other        :: *      → Bool
ne other            :: *      → Bool
not_equal other     :: *      → Bool
not_equals other    :: *      → Bool
ge i                :: Int    → Bool
greater_equal i     :: Int    → Bool
greater_equals i    :: Int    → Bool
gt i                :: Int    → Bool
greater i           :: Int    → Bool
greater_than i      :: Int    → Bool
le i                :: Int    → Bool
less_equal i        :: Int    → Bool
less_equals i       :: Int    → Bool
lt i                :: Int    → Bool
less i              :: Int    → Bool
less_than i         :: Int    → BoolString operationsappend suffix       :: String → String
strip               :: String → String
substr start end    :: String → String
take count          :: String → String
to_lower            :: String → String
to_upper            :: String → String
replace old new     :: String → String
prepend prefix      :: String → String
capitalize          :: String → String
drop count          :: String → String
duplicate           :: String → Array
contains substring  :: String → Bool
starts_with pattern :: String → Bool
startswith pattern  :: String → Bool
ends_with pattern   :: String → Bool
endswith pattern    :: String → Bool
len                 :: String → Int
length              :: String → Int
format format_str   :: *      → StringArray operationsat idx              :: Array  → String
index idx           :: Array  → String
join separator      :: Array  → String
split separator     :: String → ArrayOther operationsconst value         :: *      → *
run command         :: Array  → !
id                  :: *      → *
identity            :: *      → *