0

I wrote a simple script for generating aliases in zsh shell. Here it is:

for subject in $(find $path -maxdepth 2 -type d -print)
do
  dir=$(printf -- "$subject\n" | sed 's|.*/||')
  alias "$dir"="cd $subject"
done

It is pretty self-explanatory - recurses through a directory to up to 2 levels, and lists every directory found. Later, it uses that directory name as an alias for changing directory.

Somewhat predictably, it doesn't work. Aliases remain "encapsulated" within the script. For the record, when I execute these script lines in shell, everything works fine.

Thanks in advance.

Fedja
  • 115
  • 9
  • 1
    Source the script to execute it in the current environment instead of in it's own environment that is deleted upon termination of the script? – Kusalananda May 06 '21 at 11:05
  • @Kusalananda thanks, this is it. what do i do now, do i delete the question or? since it has no "proper" answer – Fedja May 06 '21 at 11:07
  • 1
    You probably also want to fix the for $subject in, that should be for subject in. Also, as a general rule, avoid using for loops for things like that, it will break if any results contain spaces, let alone harder things like newlines. See http://mywiki.wooledge.org/DontReadLinesWithFor – terdon May 06 '21 at 11:10
  • 2
    I closed as a duplicate of our generic "what's the difference between sourcing and executing" question since that does explain what the underlying issue was here. Since Fedja has also understood from Kusalananda's comment, that might be enough. – terdon May 06 '21 at 11:15
  • 1
    Note that the duplicate is about the bash shell, not zsh. The two shells work the same with regards to the issue in this question though. – Kusalananda May 06 '21 at 11:26

0 Answers0