Skip to content

Instantly share code, notes, and snippets.

@ormaaj
ormaaj / .bash
Created December 30, 2023 13:22
aliases
\unset -v aliasrestore
aliasrestore=$(\alias -p) aliasrestore+=${aliasrestore:+$'\n\\'unset -v aliasrestore}
\unalias -a
function setupGlobalAliases {
\alias \-- \
'--=-- ' \
'l-=local -' \
'fn=function ' \
'ts=typeset' \
@ormaaj
ormaaj / .kshrc
Last active September 11, 2023 00:16
~/.bashrc ~/.kshrc
#!/bin/false
\typeset -a bool; bool+=([1]=)
\typeset -n sh_file
\test -v KSH_VERSION
sh_file=${bool[$?]+bash_source[0]}${bool[!$?]+.sh.file}
dotFilesPath=$(\findmnt -umlnfo target -O subvol=/vol/dotfiles "PARTUUID=$(\findmnt -umlnfo partuuid -T /)")
[[ $sh_file == ?(*/)shell ]]
\source -- ${bool[!$?]+"$sh_file"}${bool[$?]+"${dotFilesPath}/common/home/shell"}/functions
rc_main "$@"
@ormaaj
ormaaj / open fd count per instance vs eager loading.bash
Last active June 10, 2023 20:40
bash builtin loader with lazy loading
$ { function f { lsfd -np "$BASHPID" --summary=only -C ' :ASSOC == "mem"'; }; typeset -fx f; f; bash -ic 'f; type -t print; print -r :\); type -t print; f; print -r :D'; } | tr -s '\n ' ' '; echo
247 31 function :) builtin 36 :D
@ormaaj
ormaaj / fmtarr.bash
Last active May 5, 2023 10:08
Array serializer with eval-based indirection for legacy shells.
#!/usr/bin/env bash
typeset -ga bool=([1]=)
# Usage: in_array name outvar
function fmtarr {
typeset x=1 y max ret=$3
printf -v "$ret" '%s+=(' "$2"
eval -- set -- "\"\${!${1}[@]}\"" "\"\${${1}[@]}\"" || return
((max = $# / 2))
function mapfile {
typeset x IFS REPLY OPTARG OPTIND=1
typeset -A ind opts=([c]=5000)
integer ind o
for x; do
getopts :d:n:O:s:u:C:c:t x || break
case $x in
\?|:) return 1 ;;
*) opts[$x]=$OPTARG ind[$x]=OPTIND
@ormaaj
ormaaj / translucent.bash
Created March 14, 2023 13:20
generate modified builtins
#!/bin/bash
shopt -s extglob lastpipe expand_aliases
shopt -u assoc_expand_once
BASH_COMPAT=51
typeset -a bool=([1]=)
function unset2 { command unset "$@"; }
function mkTranslucentFunctions {
typeset -a builtins
@ormaaj
ormaaj / .ksh
Created December 6, 2022 04:28
# Define a new type "SslCmd" with three fields: "outfile", "args", and "cmd". "cmd" has a default value of "openssl" that can be overridden by an object initializer list.
# The type defines a public method named "run".
# The result of this declaration is a new declaration command "SslCmd" that can be used in place of "typeset".
typeset -T SslCmd=(
typeset -h 'output file' outfile
typeset -h 'arguments passed to the command name passed to _.run' -a args
typeset -h 'The command to run' cmd=openssl
# A method associated with each instance that runs the command and associated args.
# _ (in this context) is a pointer to the instance, similar to "this" or "self".
#!/bin/bash
# ormaaj's init
\unalias -a
shopt -s lastpipe extglob expand_aliases
shopt -u assoc_expand_once
typeset -ga bool=([1]=)
function initializeBuiltins {
typeset -a a
@ormaaj
ormaaj / .bash
Last active November 3, 2022 05:43
#!/bin/bash
typeset -gx BASH_COMPAT=51
shopt -u assoc_expand_once
function assignArray {
typeset -n ref=$1 || return
shift
[[ ${ref@a} == *A* && "! ($# % 2)" -ne 0 ]] || return
typeset -A map
@ormaaj
ormaaj / .bash
Created October 1, 2022 09:06
flock search test
function initializeBuiltins {
local -
typeset -a a
typeset x y z
set +m
typeset libdir=${fsRoot%%+(/)}/usr/lib64
[[ -d ${libdir}/bash ]] || libdir=${rootPath%%+(/)}/lib64
for x in "${libdir}"/{ctypes.so,bash/!(hello|truefalse|mypid|necho|*.*)}; do
if [[ $(file -b --mime-type -- "$x") == 'application/x-sharedlib' && !$? -ne 0 ]]; then