0

script:

#!/bin/zsh

IFS=$'\n' arr=($(alias | grep 'git' | grep "^[g|\'g]" | sed 's/=.*//' | sed "s/'//"))

echo $arr

These two lines work in the normal command line:

~/test ·····································································································································  test-Cj2sK_tx
❮ IFS=$'\n' arr=($(alias | grep 'git' | grep "^[g|\'g]" | sed 's/=.*//' | sed "s/'//"))

~/test ·····································································································································  test-Cj2sK_tx
❯ echo $arr
g ga gaa gam gama gamc gams gamscp gap gapa gapt gau gav gb gbD gba gbd gbda gbl gbnm gbr gbs gbsb gbsg gbsr gbss gc gc!' gca gca!' gcam gcan!' gcans!' gcas gcasm gcb gcd gcf gcl gclean gcm gcmsg gcn!' gco gcount gcp gcpa gcpc gcs gcsm gd gdca gdct gdcw gds gdt gdw gf gfa gfg gfo gg gga ggpull ggpush ggsup ghh gignore gignored git-svn-dcommit-push gk gke gl glg glgg glgga glgm glgp glo glod glods glog gloga glol glola glols glp glum gm gma gmom gmt gmtvim gmum gp gpd gpf gpf!' gpoat gpristine gpsup gpu gpv gr gra grb grba grbc grbd grbi grbm grbo grbs grev grh grhh grm grmc grmv groh grrm grs grset grss grst grt gru grup grv gsb gsd gsh gsi gsps gsr gss gst gsta gstaa gstall gstc gstd gstl gstp gsts gsu gsw gswc gtl gts gtv gunignore gunwip gup gupa gupav gupv gwch gwip

However, it doesn't work when these two commands are in the script file...

~/test ·····································································································································  test-Cj2sK_tx
❯ /bin/zsh ./d-alias.sh
# nothing here...

Thank you for any help. Many thanks.

0

2 Answers 2

1

There is a much easier way to do what you want:

print -r -- ${(k)aliases[(R)*git*]}

However, as to your main question: When you do /bin/zsh ./d-alias.sh, it doesn't work, because you are starting a new shell in which to run your script. When you start a new shell from within another shell, the new shell inherits the exported environment variables from the originating shell, but nothing else. Additionally, if you start a new shell to run a script or command (as opposed to simply starting a new shell), it is not considered to be interactive. This means that your .zshrc will not get sourced and thus your aliases won't be initialized.

The solution is to instead load your script as a function:

% mkdir -p ~/Functions
% print 'print -r -- ${(k)aliases[(R)$~1]}' >> ~/Functions/d-alias
% fpath+=( ~/Functions )
% autoload -Uz d-alias
% d-alias '*git*'
g ga gaa gam gama gamc gams gamscp gap gapa gapt gau gav gb gbD gba gbd gbda gbl gbnm gbr gbs gbsb gbsg gbsr gbss gc gc!' gca gca!' gcam gcan!' gcans!' gcas gcasm gcb gcd gcf gcl gclean gcm gcmsg gcn!' gco gcount gcp gcpa gcpc gcs gcsm gd gdca gdct gdcw gds gdt gdw gf gfa gfg gfo gg gga ggpull ggpush ggsup ghh gignore gignored git-svn-dcommit-push gk gke gl glg glgg glgga glgm glgp glo glod glods glog gloga glol glola glols glp glum gm gma gmom gmt gmtvim gmum gp gpd gpf gpf!' gpoat gpristine gpsup gpu gpv gr gra grb grba grbc grbd grbi grbm grbo grbs grev grh grhh grm grmc grmv groh grrm grs grset grss grst grt gru grup grv gsb gsd gsh gsi gsps gsr gss gst gsta gstaa gstall gstc gstd gstl gstp gsts gsu gsw gswc gtl gts gtv gunignore gunwip gup gupa gupav gupv gwch gwip
0
0

Where are your aliases being defined?

If you run your script as ./name.zsh, it will run a new copy of zsh rather than a forked copy of your current shell. The new copy of zsh will source your ~/.zshrc, but not your ~/.zprofile or ~/.profile, since it is not a login shell. Edit: As Kusalananda mentions below, it won't source ~/.zshrc for scripts, only ~/.zshenv will be sourced for zsh scripts.

Pipes, loops, $(), and functions are run as forks of the current shell, and retain all of the aliases and functions, so '$(alias)' inherits the current aliases and functions, while 'zsh -c alias' does not. Running a shell script also always created a new process for the script, just like 'zsh -c alias', instead of forking the current shell.

It should work by sourcing it into the current shell:

. ./name.zsh

If your aliases are defined in ~/.zprofile or ~/.profile, then they should be moved to ~/.zshrc. which is sourced for every copy of zsh instead of just a login shell.

6
  • The zsh shell does not read ~/.zshrc for non-interactive sessions (i.e. scripts). It does however read ~/.zshenv. It's questionable whether aliases should be defined therein. The user in the question may be better helped by converting their script into a shell function.
    – Kusalananda
    Commented May 23, 2021 at 14:57
  • @Kusalananda: Thanks for the correction. I should have looked at the man page before posting. :/ I've only been using zsh for a few months; most of my experience is with Korn shell. I've updated the answer to fix the mentions of zshrc. Commented May 23, 2021 at 15:31
  • I've gotta ask... if you were using ksh, why in the world did you start using zsh? Apple ships real ATT ksh93. Commented May 23, 2021 at 16:07
  • @MarcWilson I'm using yash, zsh and bash even though my Unix doesn't ship with any of those by default. You end up using the shell(s) you feel comfortable with, for the things it/they feel most suitable for. It's a bit like asking someone why they wrote a piece of code in any particular language. I'm not forced to use C just because my system ships with a C compiler.
    – Kusalananda
    Commented May 23, 2021 at 16:48
  • Thanks, I was aking @JeffFisher. I can't imagine voluntarily switching from ksh to zsh. Commented May 23, 2021 at 18:54

Not the answer you're looking for? Browse other questions tagged .