Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expand aliases in non-interactive shells

Tags:

zsh

csh

tcsh

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.

like image 458
user137369 Avatar asked Apr 24 '14 01:04

user137369


People also ask

Which shells support aliases?

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 ~/.

What is the difference between an interactive shell and a non-interactive shell?

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.

What are shell aliases?

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.

How do I expand an alias bash script?

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.


1 Answers

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.

like image 147
ymonad Avatar answered Oct 13 '22 02:10

ymonad