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.
An bash shell alias is nothing but the shortcut to commands. The alias command allows the user to launch any command or group of commands (including options and filenames) by entering a single word. Use alias command to display a list of all defined aliases. You can add user-defined aliases to ~/.
An interactive shell reads commands from user input on a tty. Among other things, such a shell reads startup files on activation, displays a prompt, and enables job control by default. The user can interact with the shell. A shell running a script is always a non-interactive shell.
Shell aliases are shortcut names for commands. Each alias consists of one word (or even one letter) that you can use instead of a longer command line. For example, you may find yourself using the command ls -F a lot. You can easily make a shortcut for that command: lf, for example.
Also, Bash aliases are not expanded when your shell isn't interactive unless the expand_aliases shell option is set using shopt -s . You can check your current setting (on or off) by using shopt expand_aliases . instead. Generally, shell functions are preferred over aliases.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With