{"data":{"slug":"discovered-a799012a3858316448b337f7c34c1fb49ba3878ff603dedf","title":"Fish Shell","summary":"Use Fish shell syntax for scripting and command execution. Fish is the default shell, and all scripts and interactive commands must use Fish syntax unless explicitly using a bash shebang. Deliverables include Fish scripts and commands that follow Fish-specific rules.","url":"https:\/\/skillshunter.net\/skills\/discovered-a799012a3858316448b337f7c34c1fb49ba3878ff603dedf","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/discovered-a799012a3858316448b337f7c34c1fb49ba3878ff603dedf","markdown_url":"https:\/\/skillshunter.net\/skills\/discovered-a799012a3858316448b337f7c34c1fb49ba3878ff603dedf.md","download_url":"https:\/\/skillshunter.net\/skills\/discovered-a799012a3858316448b337f7c34c1fb49ba3878ff603dedf\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Ensure the shell environment is Fish (fishshell.com) before executing commands or writing scripts. Avoid using bash\/zsh\/POSIX syntax. Use `set -l` for local variables and `math` for arithmetic operations. Do not use `export` \u2014 use `set -gx` instead. Avoid using reserved variable names like `version`, `status`, or `hostname` as loop variables or for assignment.","instructions":"---\nname: fish-shell\ndescription: >\n  Fish shell scripting reference and enforcement. Activate whenever executing shell commands,\n  writing shell scripts, using run_command, or any shell interaction in the user's environment.\n  The user's shell is fish \u2014 never use bash\/zsh\/POSIX syntax in interactive commands.\n  Trigger on: any shell command, heredoc, string manipulation, variable assignment, loop,\n  conditional, arithmetic, pipe, or script writing.\n---\n\n# Fish Shell Reference\n\nThe user's shell is **fish** (<https:\/\/fishshell.com>). All interactive commands via `run_command`\nand all shell scripts MUST use fish syntax unless the script has an explicit `#!\/usr\/bin\/env bash`\nshebang (standalone scripts only).\n\n## Critical Rules\n\n1. **No heredocs** \u2014 fish has no `<< EOF`. Use multiline quoted strings instead (quotes span newlines natively)\n2. **No `$((\u2026))`** \u2014 use `math` builtin: `math $i + 1`\n3. **No `export`** \u2014 use `set -gx VAR value`\n4. **No `VAR=value`** \u2014 use `set VAR value` (except single-command override: `VAR=value command`)\n5. **No `[[ ]]`** \u2014 use `test` or `[ ]` (no `==`, use `=`)\n6. **No `${var}`** \u2014 use `$var` or `{$var}` for concatenation\n7. **No `${var%\u2026}` `${var#\u2026}` `${var\/\u2026\/\u2026}`** \u2014 use `string replace`, `string match`, `string trim`\n8. **No `$#`** \u2014 use `count $argv`\n9. **No `$?`** \u2014 use `$status`\n10. **No `$$`** \u2014 use `$fish_pid`\n11. **No `$0`** \u2014 use `status filename`\n12. **No `$(( ))` or `(( ))`** \u2014 use `math`\n13. **No `local`** \u2014 use `set -l`\n14. **No `function name() {}`** \u2014 use `function name; ...; end`\n15. **No `; do` \/ `; done` \/ `; then` \/ `; fi`** \u2014 use `; ...; end`\n16. **No `source <(cmd)`** \u2014 use `cmd | source`\n17. **No `<(cmd)`** \u2014 use `(cmd | psub)`\n18. **No `set -e` \/ `set -euo pipefail`** \u2014 fish has no equivalent; check `$status` explicitly\n19. **No multiline strings with `\\` continuation** \u2014 fish quotes span newlines naturally\n\n## Gotchas \/ Common Pitfalls\n\n### Reserved \/ Read-Only Variable Names\n\nFish has built-in variables that **cannot be used as loop variables or overwritten with `set`**.\nUsing them in `for` loops causes: `for: <name>: cannot overwrite read-only variable`.\n\nKnown read-only names to avoid:\n\n- `version` \u2014 use `ver` or `v` instead\n- `status` \u2014 use `exit_code` or `rc` instead\n- `hostname` \u2014 use `host` instead\n- `fish_pid`, `fish_trace`, `pipestatus`\n- `FISH_VERSION`, `SHLVL`\n\n### `set -l` Scoping Across Blocks\n\n`set -l` inside an `if` block is technically scoped to the enclosing function, but in practice\nvariables set inside one `if` block may not be reliably visible in a sibling `if` block.\n\n**Fix**: Declare shared variables at the top of the function, then assign inside blocks:\n\n```fish\nfunction my_func\n    # Declare at function top\n    set -l list_a\n    set -l list_b\n\n    if test -f file_a\n        set list_a (cat file_a)    # assign (no -l)\n    end\n\n    if test -f file_b\n        set list_b (cat file_b)\n        # list_a is reliably available here\n        for item in $list_b\n            contains -- $item $list_a\n        end\n    end\nend\n```\n\n## Variables\n\n```fish\n# Set local variable\nset -l name value\n\n# Set global variable\nset -g name value\n\n# Set exported (environment) variable\nset -gx NAME value\n\n# Set list (array)\nset -l mylist one two three\n\n# Append to list\nset -a mylist four\n\n# Prepend to list\nset -p mylist zero\n\n# Erase variable\nset -e name\n\n# Check if variable exists\nset -q name\n\n# Check if variable has N+ elements\nset -q name[2]\n```\n\n### Variable Expansion\n\n```fish\necho $var           # expands to all elements (one arg each)\necho \"$var\"         # joins elements with space into one arg\necho {$var}suffix   # concatenation: {$var}suffix\necho $var[1]        # first element (1-indexed!)\necho $var[-1]       # last element\necho $var[2..4]     # slice\n```\n\n**No word splitting** \u2014 fish never splits variables on whitespace. A variable with spaces stays\nas one element unless explicitly set as a list.\n\n## String Manipulation\n\nThe `string` builtin replaces `sed`, `grep`, `awk`, `tr`, `cut`, `${var%\u2026}`, etc.\n\n```fish\n# Replace (literal)\nstring replace old new $var\nstring replace -a old new $var          # all occurrences\n\n# Replace (regex)\nstring replace -r 'pattern' 'replacement' $var\nstring replace -ra '^\\s+' '' $var       # strip leading whitespace\n\n# Match (returns matches, sets $status)\nstring match -q '*.txt' $filename       # glob match, quiet\nstring match -r '(\\d+)\\.(\\d+)' $ver     # regex with capture groups\nstring match -rq 'pattern' $str        # quiet regex test\n\n# Split\nstring split ',' 'a,b,c'               # \u2192 a b c (as list)\nstring split -m1 '\/' $path             # split on first \/ only\nstring split0 (find . -print0)         # split on NUL\n\n# Join\nstring join ', ' $mylist               # \u2192 \"one, two, three\"\nstring join \\n $mylist                  # one per line\nstring join0 $mylist                   # NUL-separated\n\n# Trim\nstring trim $var                        # trim whitespace both sides\nstring trim -l $var                     # left only\nstring trim -r $var                     # right only\nstring trim -c '\/' $var                 # trim specific chars\n\n# Length\nstring length $var                      # length of each element\n\n# Substring\nstring sub -s 2 -l 3 $var              # from pos 2, length 3\nstring sub -s 2 $var                    # from pos 2 to end\n\n# Case\nstring lower $var\nstring upper $var\n\n# Pad\nstring pad -w 20 $var                   # left-pad to width 20\nstring pad -c 0 -w 5 $num              # zero-pad\n\n# Collect (prevent splitting in command substitution)\nset -l content (cat file | string collect)\n\n# Filter with --filter\necho $lines | string match --filter '*.fish'\nstring replace --filter old new $lines  # only print lines where replacement happened\n\n# Repeat\nstring repeat -n 3 'ab'                # \u2192 ababab\n\n# Escape\/unescape\nstring escape $var                      # shell-escape\nstring escape --style=url $var          # URL-encode\n```\n\n## Counting and Lists\n\n```fish\n# Count elements\ncount $mylist                           # prints count, exits 0 if >0\n\n# Count lines from command\ncmd | count\n\n# Check membership\ncontains element $mylist                # exits 0 if found\ncontains -i element $mylist             # prints index if found\ncontains -- -q $argv                    # use -- before items starting with -\n\n# Iterate\nfor item in $mylist\n    echo $item\nend\n\n# Sequence\nfor i in (seq 1 10)\n    echo $i\nend\n```\n\n## Arithmetic\n\n```fish\nmath 1 + 1                    # \u2192 2\nmath $x + 1                   # variable in expression\nmath -s0 10 \/ 3               # integer division \u2192 3\nmath -s2 10 \/ 3               # 2 decimal places \u2192 3.33\nmath \"floor($x \/ 10)\"         # functions need quotes for parens\nmath \"ceil(3.2)\"              # \u2192 4\nmath \"max($a, $b)\"            # max of two values\nmath \"$x >= 10\"               # comparison: returns 0 (true) or 1 (false) exit status\n```\n\n**Gotcha**: `*` must be quoted or escaped (`\\*` or `\"$x * 2\"`) because it's a glob character.\nUse `x` for multiplication when unquoted: `math 5 x 2`.\n\n## Control Flow\n\n```fish\n# If\/else\nif test -f $file\n    echo exists\nelse if test -d $file\n    echo directory\nelse\n    echo nope\nend\n\n# Negation\nif not test -f $file\n    echo missing\nend\n\n# Switch\nswitch $var\n    case 'pattern1'\n        echo matched\n    case 'pattern2' 'pattern3'\n        echo one of these\n    case '*'\n        echo default\nend\n\n# Combiners\ncmd1; and cmd2              # cmd2 runs only if cmd1 succeeds\ncmd1; or cmd2               # cmd2 runs only if cmd1 fails\ncmd1 && cmd2                # same as and\ncmd1 || cmd2                # same as or\n\n# For loop\nfor file in *.txt\n    echo $file\nend\n\n# While loop\nwhile read -l line\n    echo \"line: $line\"\nend < $file\n\n# Break \/ Continue\nfor i in (seq 100)\n    test $i -gt 10; and break\nend\n```\n\n## Functions\n\n```fish\nfunction greet -a name\n    echo \"Hello, $name\"\nend\n\nfunction mycommand -d \"Description for completions\"\n    # $argv contains all arguments\n    # Use argparse for option parsing:\n    argparse 'v\/verbose' 'o\/output=' -- $argv\n    or return\n\n    if set -q _flag_verbose\n        echo \"Verbose mode\"\n    end\n    echo \"Output: $_flag_output\"\nend\n```\n\n- **`$argv`** \u2014 all arguments (like bash `$@`)\n- **`$argv[1]`** \u2014 first argument (1-indexed)\n- **`-a name`** \u2014 named argument (syntactic sugar for `set -l name $argv[1]`)\n- **`return N`** \u2014 return with exit status N\n\n## Interactive Commands (run_command Best Practices)\n\nWhen using `run_command` (which requires user approval), **avoid multiline file-creation commands**.\nThey are hard to review in the approval prompt.\n\n- **Prefer IDE tools** (`write_to_file`, `edit`, `multi_edit`) to create\/modify files \u2014 no approval needed\n- **If shell is unavoidable**, use a single multiline quoted `echo` over `printf` with many args:\n\n  ```fish\n  # Readable in approval prompt\n  echo 'line one\n  line two\n  line three' > outfile\n  ```\n\n- **Never** use `printf '%s\\n' \"arg1\" \"arg2\" \"arg3\" ...` in interactive commands \u2014 it's unreadable\n\n## Multiline Strings (Heredoc Alternative)\n\nFish has no `<< EOF` heredocs, but **quotes span newlines natively** \u2014 both `'single'` and\n`\"double\"` quotes. This is the idiomatic way to handle multiline strings:\n\n```fish\n# Multiline string in a variable (single quotes \u2014 no interpolation)\nset -l msg '---\nhello world\ngoodbye world\n---'\necho $msg\n\n# Multiline string with variable interpolation (double quotes)\nset -l name \"fish\"\nset -l greeting \"Hello from $name\nThis is line two\nAnd line three\"\n\n# Multiline string piped to a command (replaces heredoc)\necho 'first line\nsecond line\nthird line' | mycommand\n\n# Write multiline to file\necho 'line one\nline two\nline three' > outfile\n\n# Alternative: printf for explicit per-line control\nprintf '%s\\n' \"line one\" \"line two\" \"line three\" > outfile\n\n# Read multiline into variable from command\nset -l content (cat file | string collect)\n```\n\n## Redirections and Pipes\n\n```fish\ncmd > file              # stdout to file\ncmd 2> file             # stderr to file\ncmd &> file             # both stdout+stderr to file\ncmd >> file             # append stdout\ncmd 2>&1                # stderr to stdout\ncmd > \/dev\/null 2>&1    # silence everything\n```\n\n## Status and Error Handling\n\n```fish\n# Fish has no set -e. Check status explicitly:\ncommand arg\nif test $status -ne 0\n    echo \"command failed\" >&2\n    return 1\nend\n\n# Or use and\/or:\ncommand arg; or begin; echo \"failed\" >&2; return 1; end\n\n# Or simply:\ncommand arg\nor return $status\n```\n\n## Process Substitution\n\n```fish\n# Bash: diff <(cmd1) <(cmd2)\ndiff (cmd1 | psub) (cmd2 | psub)\n\n# Bash: source <(cmd)\ncmd | source\n```\n\n## Common Patterns\n\n```fish\n# Bash: local var=$(cmd)\nset -l var (cmd)\n\n# Bash: var=${var:-default}\nset -q var; or set -l var default\n\n# Bash: [[ -z \"$var\" ]]\ntest -z \"$var\"\n\n# Bash: [[ -n \"$var\" ]]\ntest -n \"$var\"\n\n# Bash: for f in $(cmd); do ...; done\nfor f in (cmd)\n    ...\nend\n\n# Bash: while IFS= read -r line; do ...; done < file\nwhile read -l line\n    ...\nend < file\n\n# Bash: arr+=(\"element\")\nset -a arr element\n\n# Bash: ${#arr[@]}\ncount $arr\n\n# Bash: echo \"${arr[@]}\" | tr '\\n' ','\nstring join ',' $arr\n\n# Bash: if echo \"$var\" | grep -q pattern\nif string match -q 'pattern' $var\n\n# Bash: var=$(echo \"$var\" | sed 's\/old\/new\/g')\nset var (string replace -a old new $var)\n\n# Bash: tmpdir=$(mktemp -d); trap 'rm -rf $tmpdir' EXIT\nset -l tmpdir (mktemp -d)\n# No trap in fish \u2014 clean up manually or use a function with --on-process-exit\n```\n\n## Writing Fish Scripts\n\n```fish\n#!\/usr\/bin\/env fish\n\n# Scripts use the same syntax as interactive commands.\n# No need for set -euo pipefail \u2014 check $status where needed.\n# Functions defined in a script are available throughout that script.\n\nfunction main\n    # your code\nend\n\nmain $argv\n```\n\n## Debugging\n\n```fish\n# Trace execution (like bash set -x)\nset fish_trace 1\n\n# Profile a script\nfish --profile \/tmp\/profile.log myscript.fish\n```\n","use_cases":["Writing Fish shell scripts with Fish-specific syntax","Executing shell commands in Fish environment","Using Fish's `math` and `string` builtins for arithmetic and string manipulation","Setting and managing variables with `set` and `set -gx`","Avoiding reserved variable names in Fish shell scripts"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/damacus\/skills\/blob\/5706f70c0058822adf29da9bd7620c7ea780b9d6\/skills\/language\/fish-shell\/SKILL.md","source_commit":"5706f70c0058822adf29da9bd7620c7ea780b9d6","license":null,"version":null,"listing_approved_at":"2026-12-02","created_at":"2026-11-30T08:54:26.000000Z","updated_at":"2026-12-02T09:15:01.000000Z","seo_title":"Fish Shell Scripting and Command Reference","seo_description":"Reference for Fish shell scripting, including syntax rules, variable handling, and command execution. Use Fish-specific features like `set`, `math`, and `string` for all\u811a","seo_keywords":["fish shell","fish shell scripting","fish shell syntax","fish shell commands","fish shell variables","fish shell math","fish shell string manipulation"]}}