Expand aliases in non-interactive shells
Asked Answered
I

1

17

In bash, we can use shopt -s expand_aliases to expand aliases in scripts.

What are the equivalent commands for zsh, csh, and tcsh? Do they even exist?

In focusing my efforts on zsh, I haven’t found such a command. I even tried sourcing the file with the aliases inside the script, but it did not work.

Idaliaidalina answered 24/4, 2014 at 1:55 Comment(2)
zsh seems to expand alias in default. Could you write some example code which does not work in zsh ?Hudnall
If I have my aliases set in (for example) ~/.zshrc and I source ${HOME}/.zshrc at the top of a script, that script will not be able to use the aliases that exist in ~/.zshrc.Idaliaidalina
H
18

For zsh you can use setopt aliases

#!/usr/bin/zsh

alias hoo="echo bar"
unsetopt aliases
hoo # outputs `./test.zsh:5: command not found: hoo`
setopt aliases
hoo # outputs `bar`

see man zshoptions for detail.

For csh and tcsh, sourcing the files (source ${HOME}/.cshrc, for example) suffices.

Hudnall answered 24/4, 2014 at 3:6 Comment(2)
Figured out how to do it in csh and tcsh: using source on the relevant files suffices.Idaliaidalina
the option aliases is on by default in zsh (at least, as of Feb 2019)Notepaper

© 2022 - 2024 — McMap. All rights reserved.