In the terminal mkcd works, behind xargs it does not. xargs starts programs from the PATH – a function from your .zshrc does not exist there.
Symptom
echo /tmp/lab-y | xargs mkcd xargs: mkcd: No such file or directory
In the terminal mkcd works, behind xargs it does not.
Diagnosis
Cross-check: the script loglevels runs behind xargs – it is a program on the PATH.
echo logs/app.log | xargs loglevels | head -1 DEBUG 50
Causes
mkcdFunction instead of program
xargs starts programs. The script loglevels is one, the function mkcd only lives in your shell.
Fix
Put tools that should also run from xargs, find or other programs into an executable script in a directory on the PATH.
chmod +x ~/term-lab/bin/loglevels shellcheck ~/term-lab/bin/loglevels && echo "✔ shellcheck" loglevels logs/app.log ✔ shellcheck DEBUG 50 ERROR 50 INFO 350 WARN 50
Don't: Try to pass the function along with export -f. That is a bash feature and does not help in zsh.
▶
Break it yourself and find it – free
The Mac terminal hands-on, Day 5: aliases, functions and when a real script is better.
Go to day 5 →
The Mac terminal hands-on, Day 5: aliases, functions and when a real script is better.
Go to day 5 →
FAQ
Function or script?
Functions for things that change the running shell (cd, variables), scripts for everything other programs should call too.
Why can't mkcd be a script?
A script runs in its own process; a cd inside it does not change your shell's directory.