admin管理员组

文章数量:1352233

During some cleanup on macOS Monterey, uninstalled and reinstalled gshuf. Aliased shuf to gshuf.

The line from zshrc:

alias shuf='gshuf'

I have a tool for my music with these three lines as the total file:

shuf -n 1 /Users/joel/sh/tools/keys.txt
shuf -n 1 /Users/joel/sh/tools/timesig.txt
shuf -n 4 /Users/joel/sh/tools/notes.txt

Running the file in the terminal

keys.sh

returns

/Users/joel/sh/tools/keys.sh: line 1: shuf: command not found
/Users/joel/sh/tools/keys.sh: line 2: shuf: command not found
/Users/joel/sh/tools/keys.sh: line 3: shuf: command not found

Other aliases work fine. Restarted terminal, sourced zshrc, restarted computer. Same error.

Running keys.sh should return random-ish values from the files keys.txt, timesig.txt, and notes.txt.

During some cleanup on macOS Monterey, uninstalled and reinstalled gshuf. Aliased shuf to gshuf.

The line from zshrc:

alias shuf='gshuf'

I have a tool for my music with these three lines as the total file:

shuf -n 1 /Users/joel/sh/tools/keys.txt
shuf -n 1 /Users/joel/sh/tools/timesig.txt
shuf -n 4 /Users/joel/sh/tools/notes.txt

Running the file in the terminal

keys.sh

returns

/Users/joel/sh/tools/keys.sh: line 1: shuf: command not found
/Users/joel/sh/tools/keys.sh: line 2: shuf: command not found
/Users/joel/sh/tools/keys.sh: line 3: shuf: command not found

Other aliases work fine. Restarted terminal, sourced zshrc, restarted computer. Same error.

Running keys.sh should return random-ish values from the files keys.txt, timesig.txt, and notes.txt.

Share Improve this question edited Apr 1 at 7:56 jonrsharpe 122k30 gold badges268 silver badges475 bronze badges asked Apr 1 at 7:53 spinheadspinhead 9110 bronze badges 4
  • Aliases are normally disabled in scripts; they're intended to be used as interactive shorthands, not as general-use commands. Also, if your script doesn't have a shebang, it'll be run with /bin/sh, which (on macOS) is bash, not zsh. – Gordon Davisson Commented Apr 1 at 10:17
  • @GordonDavisson: In zsh, this is not switcheable. Aliases are always expanded, in script or interactively. – user1934428 Commented Apr 1 at 10:59
  • Using shuf at the command line works. Changing the script to call gshuf works. Seems clear that what I had installed before was shuf, not gshuf, and that in fact the alias was not expanding. I'll use gshuf in scripts and shuf at the command line. Problem solved. – spinhead Commented Apr 2 at 3:01
  • user1934428 When all other efforts have failed, I know better than to come asking for help without doing everything, including restarting my computer. Small effort to make to be absolutely sure it won't solve the problem. I don't understand anything in your entire answer, so if you care to give examples to clarify I'd love to learn. For instance, paragraph 2: by 'definitions' do you mean the alias, or something else? Which file is 'this file'? Those answers might clarify the rest of your comment. – spinhead Commented Apr 2 at 3:06
Add a comment  | 

1 Answer 1

Reset to default 1

There is no point in restarting the whole computer just because you want to edit your shell configuration.

If you put the definitions into .zshrc, you have to source this file from your script. How else could zsh then now about them? .zshrc is only sourced automatically from an interactive zsh.

However sourcing .zshrc in a script is in general a bad idea, since this file should contain definitions which are used for interactive shells, and you perhaps don't want all of them in your script. Therefore, I suggest one of two approaches:

  • Put them into ~/.zshenv . This file is automatically read by every zsh invoked under your user id.

  • Put them into a separated file, say ~/.zsh_aliases, and source this file from both your .zshrc and your script.

本文标签: macosSpecific new alias failing while existing works (even after restart)Stack Overflow